当前位置:网站首页>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);
边栏推荐
- flutter 中间一个元素,最右边一个元素
- 2022 low voltage electrician test question simulation test question bank simulation test platform operation
- 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
- How to turn off debug information in rtl8189fs
- Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
- Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing
- Opencascade7.6 compilation
- Kibana操控ES
- Golang lock
- Opengauss database backup and recovery guide
猜你喜欢
No programming code technology! Four step easy flower store applet
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
A quick understanding of analog electricity
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
剑指 Offer 62. 圆圈中最后剩下的数字
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
JPM 2021 most popular paper released (with download)
【带你学c带你飞】3day第2章 用C语言编写程序(练习 2.3 计算分段函数)
软件开发生命周期 --瀑布模型
Cesium dynamic diffusion point effect
随机推荐
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table
Leetcode face T10 (1-9) array, ByteDance interview sharing
【做题打卡】集成每日5题分享(第二期)
[learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
Redis环境搭建和使用的方法
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
[deep learning] Infomap face clustering facecluster
Questions d'entrevue
【带你学c带你飞】4day第2章 用C语言编写程序(练习 2.5 生成乘方表与阶乘表
how to come in an investnent bank team
CoordinatorLayout + TabLayout + ViewPager2(里面再嵌套一个RecyclerView),RecyclerView的滑动冲突解决
Medical management system (C language course for freshmen)
AR增强现实可应用的场景
[opencv] - comprehensive examples of five image filters
超图iServer rest服务之feature查询
Construction and maintenance of business websites [12]
leetcode2309. The best English letters with both upper and lower case (simple, weekly)