当前位置:网站首页>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);
边栏推荐
- Comparative analysis of MVC, MVP and MVVM, source code analysis
- Mathematics in Sinorgchem: computational geometry
- Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
- * and & symbols in C language
- A quick understanding of digital electricity
- [C #] use regular verification content
- A quick understanding of analog electricity
- [question] - why is optical flow not good for static scenes
- 2022 Q2 - Summary of skills to improve skills
- flutter 中間一個元素,最右邊一個元素
猜你喜欢

How to build and use redis environment

Opencascade7.6 compilation

leetcode2312. Selling wood blocks (difficult, weekly race)

CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强

How does MySQL solve the problem of not releasing space after deleting a large amount of data

Software No.1

MySQL约束与多表查询实例分析

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

Pat a-1165 block reversing (25 points)

Architecture evolution from MVC to DDD
随机推荐
离婚3年以发现尚未分割的共同财产,还可以要么
Summary of some experiences in the process of R & D platform splitting
flutter 中间一个元素,最右边一个元素
Sword finger offer II 031 Least recently used cache
essay structure
Kibana controls es
MySQL中一条SQL是怎么执行的
剑指 Offer 47. 礼物的最大价值
Additional: information desensitization;
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
[graduation season] graduate seniors share how to make undergraduate more meaningful
LFM信号加噪、时频分析、滤波
No programming code technology! Four step easy flower store applet
Questions d'entrevue
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
If you want to rewind the video picture, what simple methods can you use?
[liuyubobobo play with leetcode algorithm interview] [00] Course Overview
[question] - why is optical flow not good for static scenes
As a software testing engineer, will you choose the bank post? Laolao bank test post
CSDN article underlined, font color changed, picture centered, 1 second to understand