Android access scanning function (cameraX + zxing)

Time:2021-12-30

How to integrate:

1. In the root directory of build Add jitpack dependency to gradle:

    allprojects {
      repositories {
          maven { url "https://jitpack.io" }
       }
  }

2. In project build Add yxing dependency to gradle:

 implementation 'com.github.amggg:YXing:V1.0.6'
Function:

1. Scan QR code and barcode.
2. Generate QR code with logo QR code and barcode.
3. Identify the QR code pictures in the album.

matters needing attention:

1. Before entering the code scanning interface, dynamically request camera permission by yourself.
2. Minsdk > = 21 (Android 5.0 and above).
3.AndroidX。

How to use:

1. Simple call:
Start in activity:

ScanCodeConfig.create(MainActivity.this)
                                //Set the code scanning page style scanstyle None: no scanstyle QQ: imitation QQ style scanstyle Wechat: imitation wechat style scanstyle Customize: custom styles
                                .setStyle(style)
                                //Scan the code successfully, whether to play the sound effect true: play false: do not play
                                .setPlayAudio(false)
                                .buidler()
                                //Jump to the code scanning page. The code scanning page can customize the style
                                .start(ScanCodeActivity.class);

Start in fragment:

ScanCodeConfig.create(MainActivity.this, mFragment)
                                //Set the code scanning page style scanstyle None: no scanstyle QQ: imitation QQ style scanstyle Wechat: imitation wechat style scanstyle Customize: custom styles
                                .setStyle(style)
                                //Scan the code successfully, whether to play the sound effect true: play false: do not play
                                .setPlayAudio(false)
                                .buidler()
                                //Jump to the code scanning page. The code scanning page can customize the style
                                .start(ScanCodeActivity.class);

Obtain scanning results:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //Receive code scanning results
    if(resultCode == RESULT_OK && requestCode == ScanCodeConfig.QUESTCODE && data != null){
        Bundle extras = data.getExtras();
        if(extras != null){
            String code = extras.getString(ScanCodeConfig.CODE_KEY);
            tvCode. Settext (string. Format ("% s% s", "result:", code));
        }
    }
}

There are two built-in styles for use, which can be set through the setstyle method.
1. Imitation QQ

Android access scanning function (cameraX + zxing)

2. Imitation wechat

Android access scanning function (cameraX + zxing)

By setting style to scanstyle Customize for custom style design:

ScanCodeConfig.create(MainActivity.this)
                                //Set the code scanning page style scanstyle None: no scanstyle QQ: imitation QQ style scanstyle Wechat: imitation wechat style scanstyle Customize: custom styles
                                .setStyle(ScanStyle.CUSTOMIZE)
                                //Scan the code successfully, whether to play the sound effect true: play false: do not play
                                .setPlayAudio(true)
                                //Set sound effects audio
                                .setAudioId(R.raw.beep)
                                ////////////////////////////////////////////
                                //The following configuration is scanstyle Effective when customize
                               //Set code scanning frame position scansize: code scanning frame size width height 1:1 offsetx: X-axis offset offsety: Y-axis offset unit / PX
                                .setScanSize(800, 0, -200)
                                //Set the position of the code scanning box left: the position on the left of the border top: the position on the top of the border right: the position on the right of the border bottom: the position under the border isusepx: whether to use PX unit true: use (PX) false: do not use (DP) default false
//                                    .setScanRect(new ScanRect(50, 200, 300, 450), false)
                                //Whether to display the four corner marks on the border true: display false: not display
                                .setShowFrame(true)
                                //Sets the color of the four corner markers on the border
                                .setFrameColor(R.color.whilte)
                                //Set four corner mark fillet units on the border / DP
                                .setFrameRaduis(2)
                                //Sets the width units of the four corners on the border / DP
                                .setFrameWith(4)
                                //Set the length unit of the four corners on the border / DP
                                .setFrameLenth(15)
                                //Set whether to display border external shadow true: display false: do not display
                                .setShowShadow(true)
                                //Sets the shadow color outside the border
                                .setShaowColor(R.color.black_tran30)
                                //Set the motion mode of code scanning bar scanmode Reverse: reciprocating motion scanmode Restart: repeat motion, default scanmode RESTART
                                .setScanMode(ScanMode.REVERSE)
                                //Set the time unit for scanning the code bar once / MS, and the default is 3000
                                .setScanDuration(3000)
                                //Set scan bar picture
                                .setScanBitmapId(R.mipmap.scan_wechatline)
                                //////////////////////////////////////////////
                                .buidler()
                                //Jump to the code scanning page. The code scanning page can customize the style
                                .start(ScanCodeActivity.class);

<meta charset=”utf-8″>

Android access scanning function (cameraX + zxing)

Since the code scanning interface generally has many different business logic, you can customize the code scanning interface according to your needs:

Custom code scanning interface process:
1. Create a new activity and inherit scancodeactivity

public class MyScanActivity extends ScanCodeActivity 

2. Rewrite getlayoutid () and initdata () methods
Getlayoutid returns the ID of the layout file defined by you
Initdata() initializes data listening as usual, and so on
public class MyScanActivity extends ScanCodeActivity {

private AppCompatButton btnOpenFlash;

@Override
public int getLayoutId() {
    return R.layout.activity_myscan;
}

@Override
public void initData() {
    super.initData();
    btnOpenFlash = findViewById(R.id.btn_openflash);

    btnOpenFlash.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            isOpenFlash = !isOpenFlash;
            setFlashStatus(isOpenFlash);
            btnOpenFlash. Settext (isepenflash? "Turn off flash": "turn on flash");
        }
    });
}

}
3. Copy the following code into the layout file:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlparent"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
      <androidx.camera.view.PreviewView
        android:id="@+id/pvCamera"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
<!-- Start customizing interface -- >

</RelativeLayout>

Previewview is a code scanning interface. You can add your own layout freely below:
Here’s a try:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlparent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.camera.view.PreviewView
    android:id="@+id/pvCamera"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Start customizing interface -- >

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/btn_openflash"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    Android: text = "flash on"
    android:layout_alignParentBottom="true"
    />

</RelativeLayout>

4. Replace the start () method parameter with a custom activity:

ScanCodeConfig.create(MainActivity.this)
                                //Set the code scanning page style scanstyle None: no scanstyle QQ: imitation QQ style scanstyle Wechat: imitation wechat style
                                .setStyle(style)
                                //Scan the code successfully, whether to play the sound effect true: play false: do not play
                                .setPlayAudio(true)
                                .buidler()
                                //Jump to the code scanning page. The code scanning page can customize the style
                                .start(MyScanActivity.class);

Preview:

Android access scanning function (cameraX + zxing)

<meta c<meta charset=”utf-8″>

<article>

In addition to the code scanning function, two-dimensional codes can also be generated:
1. Separate QR Code:

Bitmap bitmap = ScanCodeConfig.createQRCode("star");

2. QR code with logo:

Bitmap bitmap = ScanCodeConfig.createQRcodeWithLogo("star", BitmapFactory.decodeResource(getResources(), R.mipmap.timg));

3. Generate QR code with stroked logo

Bitmap bitmap = ScanCodeConfig.createQRcodeWithStrokLogo("star", SizeUtils.dp2px(getApplicationContext(), 200), BitmapFactory.decodeResource(getResources(), R.mipmap.timg), SizeUtils.dp2px(getApplicationContext(), 60), SizeUtils.dp2px(getApplicationContext(), 60), SizeUtils.dp2px(getApplicationContext(), 10), SizeUtils.dp2px(getApplicationContext(), 10), SizeUtils.dp2px(getApplicationContext(), 2), ContextCompat.getColor(MainActivity.this, R.color.colorAccent));

The width and height of QR code, logo and fillet can be set by yourself:

/**
     *Generate QR code
     *
     *@ param text text text, web address, etc. to generate QR code
     *@ param size size ()
     * @return bitmap
     */
    public static Bitmap createQRCode(String text, int size) {
/**Generate QR code with logo
     *@ param text
     *@ param size QR code size 1:1
     * @param logo   logo
     *@ param logowith logo width
     *@ param logohigh
     *@ param logoraduisx logo x fillet
     *@ param logoraduisy logo
     * @return
     */
    public static Bitmap createQRcodeWithLogo(String text, int size, Bitmap logo, int logoWith, int logoHigh, float logoRaduisX, float logoRaduisY){
/**Generate 2D code with stroked logo
     *@ param text
     *@ param size QR code size 1:1
     * @param logo   logo
     *@ param logowith logo width
     *@ param logohigh
     *@ param logoraduisx logo x fillet
     *@ param logoraduisy logo
     *@ param storkwith stroke width
     *@ param storkcolor stroke color
     * @return
     */
    public static Bitmap createQRcodeWithStrokLogo(String text, int size, Bitmap logo, int logoWith, int logoHigh, float logoRaduisX, float logoRaduisY, int storkWith, int storkColor){

In addition to generating QR codes, it is also essential to identify QR codes from Photo Albums:
Call the following method to pass in the selected image URI to get the content of the QR code..

/**
     *Decoding URI QR code picture
     * @return
     */
    public static String scanningImage(Activity mActivity, Uri uri) {

4. Generate barcode

Bitmap barCode = ScanCodeConfig.createBarcode("234323423423", 500, 200, false);

You can configure the width and height of the barcode and whether to display the content below the barcode.
Note: the barcode content cannot be Chinese.

/**
     *Generate barcode
     *@ param content the content contained in the barcode to be generated
     *@ param widthpix bar code width
     *@ param highpix bar code height
     *@ param isshowcontent whether to display the content contained in the barcode
     *@ return returns the bitmap that generated the bar
     */
    public static Bitmap createBarcode(String content, int widthPix, int heightPix, boolean isShowContent) {

There is an app download link in GitHub

Android access scanning function (cameraX + zxing)

Attach GitHub link:https://github.com/amggg/YXing