当前位置:网站首页>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);
边栏推荐
- C # use system data. The split mixed mode assembly is generated for the "v2.0.50727" version of the runtime, and it cannot be loaded in the 4.0 runtime without configuring other information
- 使用开源项目【Banner】实现轮播图效果(带小圆点)
- trading
- Opengauss database backup and recovery guide
- leetcode373. Find and minimum k-pair numbers (medium)
- Sword finger offer 31 Stack push in and pop-up sequence
- 2022 low voltage electrician test question simulation test question bank simulation test platform operation
- flutter 中間一個元素,最右邊一個元素
- 【带你学c带你飞】2day 第8章 指针(练习8.1 密码开锁)
- Redis环境搭建和使用的方法
猜你喜欢

How to use redis ordered collection
![[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)](/img/8e/a86a9724251718d98ce172a6a96e53.png)
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)

leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)

【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)

An analysis of circuit for quick understanding

Spend a week painstakingly sorting out the interview questions and answers of high-frequency software testing / automated testing

Sword finger offer 62 The last remaining number in the circle

Pat a-1165 block reversing (25 points)

A quick understanding of analog electricity

CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
随机推荐
[opencv] - comprehensive examples of five image filters
[liuyubobobo play with leetcode algorithm interview] [00] Course Overview
剑指 Offer 47. 礼物的最大价值
How to solve MySQL master-slave delay problem
[learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
[deep learning] Infomap face clustering facecluster
Construction and maintenance of business websites [13]
Construction and maintenance of business websites [14]
Es interview questions
Architecture evolution from MVC to DDD
C # use system data. The split mixed mode assembly is generated for the "v2.0.50727" version of the runtime, and it cannot be loaded in the 4.0 runtime without configuring other information
Sword finger offer 47 Maximum value of gifts
A quick understanding of analog electricity
【OpenCV】-5种图像滤波的综合示例
Software testing learning notes - network knowledge
Construction and maintenance of business websites [12]
Mathematics in Sinorgchem: computational geometry
Pat a-1165 block reversing (25 points)
Architecture evolution from MVC to DDD
Construction and maintenance of business websites [15]