当前位置:网站首页>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);
边栏推荐
- leetcode2312. Selling wood blocks (difficult, weekly race)
- Calculation (computer) code of suffix expression
- how to come in an investnent bank team
- RTL8189FS如何关闭Debug信息
- flutter 中间一个元素,最右边一个元素
- Questions d'entrevue
- 2022安全员-C证考试题及模拟考试
- 大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
- 2022 low voltage electrician test question simulation test question bank simulation test platform operation
- Sword finger offer 31 Stack push in and pop-up sequence
猜你喜欢

leetcode373. 查找和最小的 K 对数字(中等)

Opengauss database backup and recovery guide
![[liuyubobobo play with leetcode algorithm interview] [00] Course Overview](/img/1c/c8cab92c74b6658c3ef608c5255f1f.png)
[liuyubobobo play with leetcode algorithm interview] [00] Course Overview

Pat a-1165 block reversing (25 points)
![[learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)](/img/39/42b1726e5f446f126a42d7ac673dce.png)
[learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)

How to batch add background and transition effects to videos?

Word search applet design report based on cloud development +ppt+ project source code + demonstration video

leetcode373. Find and minimum k-pair numbers (medium)
![[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table](/img/f4/298f64c4b4f8674eda4e8fb19a976a.png)
[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table

How to turn off debug information in rtl8189fs
随机推荐
Types of exhibition items available in the multimedia interactive exhibition hall
Software development life cycle -- waterfall model
A quick understanding of analog electricity
剑指 Offer 29. 顺时针打印矩阵
[reading notes] programmer training manual - practical learning is the most effective (project driven)
Openssl3.0 learning XXI provider encoder
WebGPU(一):基本概念
[C #] use regular verification content
JPM 2021 most popular paper released (with download)
JS slow animation
Pytest testing framework
Construction and maintenance of business websites [13]
Vsocde has cli every time it is opened js
how to add one row in the dataframe?
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
Sword finger offer 29 Print matrix clockwise
Sword finger offer 42 Maximum sum of continuous subarrays
[learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)
C write TXT file
OpenCASCADE7.6编译