当前位置:网站首页>Realize the code scanning function of a custom layout
Realize the code scanning function of a custom layout
2022-07-02 02:22:00 【Rannki】
- Open source project address :https://github.com/jenly1314/ZXingLite
- stay Project Of build.gradle Add remote warehouse inside
allprojects { repositories { //... mavenCentral() } } - stay Module Of build.gradle Add and import dependencies
implementation 'com.github.jenly1314:zxing-lite:2.1.1'
There are several ways to scan code quickly :
1、 Use it directly CaptureActivity perhaps CaptureFragment.( Pure code scanning , No additives )
2、 By inheritance CaptureActivity perhaps CaptureFragment And customize the layout .( Applicable to most scenarios , There is no need to care about the logic of scanning code , When customizing the layout, you need to overwrite getLayoutId Method ) Implementation example :CustomCaptureActivity and QRCodeActivity
3、 In your project Activity perhaps Fragment Instantiate a CameraScan that will do .( It is suitable for writing interactive logic in the code scanning interface , Because of the project structure or other reasons , Cannot inherit directly or indirectly CaptureActivity or CaptureFragment When using ) Implementation example :CustomActivity
4、 Inherit CameraScan Realize one by yourself , You can refer to the default implementation class DefaultCameraScan, Other steps are the same 3.( Extended advanced usage , Use caution )
Example layout
Customizable layout ( overwrite getLayoutId Method ), At least there should be PreviewView.
PreviewView Used to preview , At least there should be PreviewView, If it's inheritance CaptureActivity or CaptureFragment, Control id Overwritable getPreviewViewId Method customization
ViewfinderView Used to render scanning view , Give users a visual effect , It doesn't matter if you scan the code and recognize it , If it's inheritance CaptureActivity or CaptureFragment, Control id rewritable getViewfinderViewId Method customization , The default is previewView, return 0 Indicates that there is no need to ViewfinderView
ivFlashlight Used for built-in flashlight , If it's inheritance CaptureActivity or CaptureFragment, Control id rewritable getFlashlightId Method customization , The default is ivFlashlight. return 0 It means there is no need for a built-in flashlight . You can also define it yourself
Code example ( QR code / Bar code )
// Jump to the default scanning interface
startActivityForResult(new Intent(context,CaptureActivity.class),requestCode);
// Generate qr code
CodeUtils.createQRCode(content,600,logo);
// Generate barcode
CodeUtils.createBarCode(content, BarcodeFormat.CODE_128,800,200);
// Parse barcode / QR code
CodeUtils.parseCode(bitmap);
// Analysis of QR code
CodeUtils.parseQRCode(bitmap);Personal practice records :
Create a new one activity Inherit CaptureActivity.
package com.efuture.androidmvvmdemo.activity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.efuture.androidmvvmdemo.R;
import com.gyf.immersionbar.ImmersionBar;
import com.king.zxing.CaptureActivity;
import com.king.zxing.util.CodeUtils;
import java.io.FileNotFoundException;
public class QrCodeActivity extends CaptureActivity implements View.OnClickListener {
// Photo album
private ImageView iv_photo;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the status bar color to white
ImmersionBar.with(this).statusBarColor(R.color.black)
.fitsSystemWindows(true).init();
iv_photo = findViewById(R.id.iv_photo);
iv_photo.setOnClickListener(this);
}
@Override
public int getLayoutId() {
return R.layout.activity_qr_code;
}
@Override
public int getFlashlightId() {
return R.id.ll_Flashlight;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// Photo album
case R.id.iv_photo:
// Open album
Intent intent = new Intent(Intent.ACTION_PICK);
// Specify that what you get is a picture
intent.setType("image/*");
startActivityForResult(intent, 1);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
// If you return from the album
case 1:
if (data != null) {
Uri uris;
uris = data.getData();
Bitmap bitmap = null;
// Uri Turn into Bitmap
try {
bitmap = getBitmapFromUri(uris);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Generate qr code
CodeUtils.createQRCode("aaaaa", 600, bitmap);
// Analysis of QR code
String s = CodeUtils.parseQRCode(bitmap);
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
}
break;
}
}
// Uri Turn into Bitmap
private Bitmap getBitmapFromUri(Uri uri) throws FileNotFoundException {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
return bitmap;
}
}This is the layout file :
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.king.zxing.ViewfinderView
android:id="@+id/viewfinderView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<androidx.appcompat.widget.AppCompatImageView
android:layout_marginTop="21dp"
android:id="@+id/iv_new_back"
android:layout_width="24dp"
android:layout_height="22dp"
android:layout_marginStart="12dp"
android:src="@mipmap/return_white"/>
<TextView
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Open pre Duobao , You can scan the code and pay at a glance "
android:textColor="@color/personal_word_color"
android:textSize="17sp"
android:gravity="center"
android:layout_gravity="center" />
<LinearLayout
android:id="@+id/ll_Flashlight"
android:layout_centerHorizontal="true"
android:layout_below="@+id/tv_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_marginTop="120dp"
android:id="@+id/ivFlashlight"
android:layout_width="30dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="@drawable/zxl_flashlight_selector"/>
<TextView
android:layout_below="@+id/ivFlashlight"
android:layout_centerHorizontal="true"
android:id="@+id/tv_illuminate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Touch to illuminate "
android:textColor="@color/white"
android:textSize="17sp"
android:gravity="center"
android:layout_gravity="center" />
</LinearLayout>
<RelativeLayout
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_Payment"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@mipmap/icon_detail_star" />
<TextView
android:textColor="@color/white"
android:layout_below="@+id/iv_Payment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Payment code " />
<ImageView
android:layout_alignParentEnd="true"
android:id="@+id/iv_photo"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@mipmap/icon_detail_star" />
<TextView
android:layout_alignParentEnd="true"
android:textColor="@color/white"
android:layout_below="@+id/iv_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Photo album " />
</RelativeLayout>
</RelativeLayout>Jump to this activity, The code scanning interface will open .
// Skip the code scanning interface startActivityForResult(new Intent(requireContext(), QrCodeActivity.class), 1);
边栏推荐
- 剑指 Offer 42. 连续子数组的最大和
- Cesium dynamic diffusion point effect
- How to turn off debug information in rtl8189fs
- OpenCASCADE7.6编译
- [graduation season] graduate seniors share how to make undergraduate more meaningful
- How to use redis ordered collection
- Sword finger offer 47 Maximum value of gifts
- [learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
- Divorce for 3 years to discover the undivided joint property, or
- What is the function of the headphone driver
猜你喜欢

As a software testing engineer, will you choose the bank post? Laolao bank test post

Types of exhibition items available in the multimedia interactive exhibition hall

超图iServer rest服务之feature查询

LFM信号加噪、时频分析、滤波

AR增强现实可应用的场景

花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案

软件开发生命周期 --瀑布模型

Architecture evolution from MVC to DDD

query词权重, 搜索词权重计算

Sword finger offer 62 The last remaining number in the circle
随机推荐
[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table
DNS domain name resolution
No programming code technology! Four step easy flower store applet
flutter 中間一個元素,最右邊一個元素
The wave of layoffs in big factories continues, but I, who was born in both non undergraduate schools, turned against the wind and entered Alibaba
LFM signal denoising, time-frequency analysis, filtering
Sword finger offer II 031 Least recently used cache
What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
Opengauss database backup and recovery guide
MySQL constraints and multi table query example analysis
使用开源项目【Banner】实现轮播图效果(带小圆点)
2022低压电工考试题模拟考试题库模拟考试平台操作
golang---锁
大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
Sword finger offer 47 Maximum value of gifts
2022 safety officer-c certificate examination questions and mock examination
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
Openssl3.0 learning XXI provider encoder
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
2022安全员-C证考试题及模拟考试