Here he comes, here he comes, here he comes with YXing. YXing integrates CameraX + Zxing, simple call, perfect function, custom scan code interface, no longer need to CV a bunch of redundant Camera code into the project like before.
1. Add jitPack dependency to build.gradle in root directory:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
Copy the code
2. Add YXing dependency to project build.gradle:
Implementation 'com. Making. Amggg: YXing: V1.0.2'Copy the code
Features: 1. Scan code function. 2. Generate QR code and QR code with LOGO (size rounded corner can be set). 3. Identify the QR code pictures in the album.
Note: 1. Before entering the scanning code boundary, dynamically request the camera permission. 2. MinSdk >= 21 (android5.0 and above)
How to use: 1. Simple call:
Scancodeconfig. create(mainactive. this) // Set the scan page style scanstyle. NONE: NONE scanstyle. QQ: copy THE QQ style ScanStyle. CUSTOMIZE: CUSTOMIZE style.setstyle (style) // Whether sound effects are played after the code is scanned true: play false: CUSTOMIZE .setPlayAudio(false).buidler() // Skip to scan page. Start (scancodeActive.class);Copy the code
Obtain the scan result:
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); If (resultCode == RESULT_OK && requestCode == ScanCodeConfig.QUESTCODE && data! = null){ Bundle extras = data.getExtras(); if(extras ! = null){ String code = extras.getString(ScanCodeConfig.CODE_KEY); Format ("%s%s", "result: ", code); tvcode.settext (String.format("%s%s"," result: ", code)); }}}Copy the code
Two built-in styles are available, set by the setStyle method. 1, imitation QQ
2. Fake wechat
CUSTOMIZE the style design by setting the style to scanstyle. CUSTOMIZE:
Scancodeconfig. create(mainactive. this) // Set the scan page style scanstyle. NONE: NONE scanstyle. QQ: copy THE QQ style ScanStyle. CUSTOMIZE: CUSTOMIZE style.setstyle (scanstyle.customize) // Whether sound effects are played after code scanning is successful True: play false: CUSTOMIZE Don't play. SetPlayAudio (true). / / set sound audio setAudioId (R.r aw. Beep) / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / the following configuration in style CUSTOMIZE // Set the position of the scan box left: the left position of the border top: the top position of the border Right: the right position of the border Bottom: SetScanRect (new ScanRect(50, 200, 300, 450)) // Whether to display the four corners of the border Not display.setShowFrame (true) // Set the color of the four corner markers. SetFrameColor (r.color.whilte) // Set the unit of the four corner markers/dp.setFrameraduis (2) Dp. setFrameLenth(15) // Set whether to display the outer shadow of the frame true: display false: Do not display.setshowShadow (true) // Set the border outer shadow color. SetShaowColor (r.color.black_tran30) // Set the scan bar image SetScanBitmapId (R.m ipmap. Scan_wechatline) / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /. Buidler () / / jump code page Code page can custom style .start(MyScanActivity.class);Copy the code
The scan interface generally has many different service logics, so you can customize the scan interface as required:
1. Create an Activity and inherit ScanCodeActivity
public class MyScanActivity extends ScanCodeActivity
Copy the code
2. Override getLayoutId() and initData() methods getLayoutId returns your own layout file ID initData() initializes data listening as usual, etc
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) { Toast.maketext (myscanactivity.this, "Turn on the flash ", toast.length_short).show(); }}); }}Copy the code
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" /> <! </RelativeLayout>Copy the code
PreviewView is a scan interface, you can add your own layout:
<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 the custom interface - > < androidx. Appcompat. Widget. AppCompatButton android: id = "@ + id/btn_openflash" Android :layout_width="match_parent" Android :layout_height="wrap_content" Android :text=" Flash" android:layout_alignParentBottom="true" /> </RelativeLayout>Copy the code
4. Replace the start() method argument with a custom Activity:
Scancodeconfig. create(mainactive. this) // Set the scan page style scanstyle. NONE: NONE scanstyle. QQ: copy THE QQ style ScanStyle. SetStyle (style) // Whether sound is played successfully true: play false: .setPlayAudio(true).buidler() // skip scan page. Start (myscanactivity.class);Copy the code
Preview:
In addition to the scan function, it can also generate two-dimensional code: 1.
Bitmap bitmap = ScanCodeConfig.createQRCode("star");
ivCode.setImageBitmap(bitmap);
Copy the code
2. Qr code with logo:
Bitmap bitmap = ScanCodeConfig.createQRcodeWithLogo("star", BitmapFactory.decodeResource(getResources(), R.mipmap.timg));
ivCode.setImageBitmap(bitmap);
Copy the code
Qr code width and height, LOGO width and height, rounded corners can be set by themselves:
@param size Specifies the size of the QR code to be generated. () * @return bitmap */ public static bitmap createQRCode(String text, int size) {Copy the code
/** Generate qr code with logo * @param text * @param size QR code size 1: 1 * @param logo Logo * @param logoWith logo width * @param logoHigh Logo height * @param logoRaduisX Logo X Rounded corners * @param logoRaduisY @return */ public static Bitmap createQRcodeWithLogo(String text, int size, Bitmap logo, int logoWith, int logoHigh, float logoRaduisX, float logoRaduisY){Copy the code
In addition to the generation of two-dimensional code, from the album to identify the two-dimensional code is also essential: call the following method, the selected picture URI in you can get the content of the two-dimensional code.
Public static String scanningImage(Activity mActivity, uri uri) {Copy the code
Call it a day
Github has a link to download the app
Technology is not strong, focus on finishing, do not like spray.
Attach the Github link to github.com/amggg/YXing
star star star