Android实现扫码功能
程序员文章站
2022-04-01 08:12:49
本文实例为大家分享了android实现扫码功能的具体代码,供大家参考,具体内容如下1、引入implementation 'com.journeyapps:zxing-android-embedded:...
本文实例为大家分享了android实现扫码功能的具体代码,供大家参考,具体内容如下
1、引入
implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
2、使用:
public void initscan() { intentintegrator integrator = new intentintegrator(this); // 设置要扫描的条码类型,one_d_code_types:一维码,qr_code_types-二维码 integrator.setdesiredbarcodeformats(intentintegrator.qr_code_types); integrator.setcaptureactivity(scanactivity.class); //设置打开摄像头的activity integrator.setprompt(""); //底部的提示文字,设为""可以置空 integrator.setcameraid(0); //前置或者后置摄像头 integrator.setbeepenabled(true); //扫描成功的「哔哔」声,默认开启 integrator.setbarcodeimageenabled(true); integrator.initiatescan(); } @override protected void onactivityresult(int requestcode, int resultcode, intent intent) { super.onactivityresult(requestcode, resultcode, intent); if (requestcode == intentintegrator.request_code) { intentresult scanresult = intentintegrator.parseactivityresult(requestcode, resultcode, intent); if (scanresult != null && scanresult.getcontents() != null) { string result = scanresult.getcontents(); logutil.d("扫码返回: " + result); try { jsonobject jsonobject = new jsonobject(result); if (jsonobject.has(constant.userpolicemenid)) { //todo 逻辑 } else { toastutil.showshorttoast("未扫描到有效的信息"); } } catch (exception e) { toastutil.showshorttoast("未扫描到有效的信息"); e.printstacktrace(); } } else { toastutil.showshorttoast("未扫描到有效的信息"); } } }
3、scanactivity
public class scanactivity extends backactivity { @bindview(r.id.dbv) decoratedbarcodeview mdbv; private capturemanager capturemanager; @override protected int getlayoutid() { getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); return r.layout.activity_scan; } @override protected void init() { super.init(); capturemanager = new capturemanager(this, mdbv); capturemanager.initializefromintent(getintent(), getsavedinstancestate()); capturemanager.decode(); } @override public void onsaveinstancestate(@notnull bundle outstate, @notnull persistablebundle outpersistentstate) { super.onsaveinstancestate(outstate, outpersistentstate); capturemanager.onsaveinstancestate(outstate); } @override public boolean onkeydown(int keycode, keyevent event) { return mdbv.onkeydown(keycode, event) || super.onkeydown(keycode, event); } @override protected void onpause() { super.onpause(); capturemanager.onpause(); } @override public void onresume() { super.onresume(); capturemanager.onresume(); } @override protected void ondestroy() { super.ondestroy(); capturemanager.ondestroy(); } }
4、布局文件
activity_scan
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".ui.main.alarmassistant.scanactivity"> <com.journeyapps.barcodescanner.decoratedbarcodeview android:id="@+id/dbv" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" app:zxing_framing_rect_height="200dp" app:zxing_framing_rect_width="200dp" app:zxing_preview_scaling_strategy="fitxy" app:zxing_scanner_layout="@layout/view_qr" app:zxing_use_texture_view="true" /> </linearlayout>
view_qr
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <com.journeyapps.barcodescanner.barcodeview android:id="@+id/zxing_barcode_surface" android:layout_width="match_parent" android:layout_height="match_parent" app:zxing_framing_rect_height="50dp" app:zxing_framing_rect_width="250dp" /> <com.x.x.widget.qrview android:id="@+id/zxing_viewfinder_view" android:layout_width="match_parent" android:layout_height="match_parent" app:zxing_possible_result_points="@color/color_white" app:zxing_result_view="@color/zxing_custom_result_view" app:zxing_viewfinder_laser="@color/color_white" app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask" /> <textview android:id="@+id/zxing_status_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:layout_marginbottom="30dp" android:background="@color/zxing_transparent" android:text="@string/zxing_msg_default_status" android:textcolor="@color/zxing_status_text" /> <com.x.common.widget.myactionbar android:id="@+id/action_bar" android:layout_width="match_parent" android:layout_height="wrap_content" app:dark_mode="true" app:title="扫一扫" /> </merge>
5、
/** * 自定义扫描界面 */ public class qrview extends viewfinderview { public int laserlineposition = 0; public float[] position = new float[]{0f, 0.5f, 1f}; public int[] colors = new int[]{0x0027b14d, 0xff27b14d, 0x0027b14d}; public lineargradient lineargradient; private int screenrate; public qrview(context context, attributeset attrs) { super(context, attrs); float density = context.getresources().getdisplaymetrics().density; screenrate = (int) (15 * density); } @override public void ondraw(canvas canvas) { int corner_width = 15; refreshsizes(); if (framingrect == null || previewframingrect == null) { return; } rect frame = framingrect; rect previewframe = previewframingrect; int width = canvas.getwidth(); int height = canvas.getheight(); //绘制4个角 paint.setcolor(getresources().getcolor(r.color.color_white));//定义画笔的颜色 canvas.drawrect(frame.left, frame.top, frame.left + screenrate, frame.top + corner_width, paint); canvas.drawrect(frame.left, frame.top, frame.left + corner_width, frame.top + screenrate, paint); canvas.drawrect(frame.right - screenrate, frame.top, frame.right, frame.top + corner_width, paint); canvas.drawrect(frame.right - corner_width, frame.top, frame.right, frame.top + screenrate, paint); canvas.drawrect(frame.left, frame.bottom - corner_width, frame.left + screenrate, frame.bottom, paint); canvas.drawrect(frame.left, frame.bottom - screenrate, frame.left + corner_width, frame.bottom, paint); canvas.drawrect(frame.right - screenrate, frame.bottom - corner_width, frame.right, frame.bottom, paint); canvas.drawrect(frame.right - corner_width, frame.bottom - screenrate, frame.right, frame.bottom, paint); // 画出外部(即构图矩形之外)变暗 paint.setcolor(resultbitmap != null ? resultcolor : maskcolor); canvas.drawrect(0, 0, width, frame.top, paint); canvas.drawrect(0, frame.top, frame.left, frame.bottom, paint); canvas.drawrect(frame.right, frame.top, width, frame.bottom, paint); canvas.drawrect(0, frame.bottom, width, height, paint); if (resultbitmap != null) { // draw the opaque result bitmap over the scanning rectangle paint.setalpha(current_point_opacity); canvas.drawbitmap(resultbitmap, null, frame, paint); } else { laserlineposition = laserlineposition + 8; if (laserlineposition >= frame.height()) { laserlineposition = 0; } lineargradient = new lineargradient(frame.left + 1, frame.top + laserlineposition, frame.right - 1, frame.top + 10 + laserlineposition, colors, position, shader.tilemode.clamp); // draw a red "laser scanner" line through the middle to show decoding is active paint.setshader(lineargradient); //绘制扫描线 canvas.drawrect(frame.left + 1, frame.top + laserlineposition, frame.right - 1, frame.top + 10 + laserlineposition, paint); paint.setshader(null); float scalex = frame.width() / (float) previewframe.width(); float scaley = frame.height() / (float) previewframe.height(); list<resultpoint> currentpossible = possibleresultpoints; list<resultpoint> currentlast = lastpossibleresultpoints; int frameleft = frame.left; int frametop = frame.top; if (currentpossible.isempty()) { lastpossibleresultpoints = null; } else { possibleresultpoints = new arraylist<>(5); lastpossibleresultpoints = currentpossible; paint.setalpha(current_point_opacity); paint.setcolor(resultpointcolor); for (resultpoint point : currentpossible) { canvas.drawcircle(frameleft + (int) (point.getx() * scalex), frametop + (int) (point.gety() * scaley), point_size, paint); } } if (currentlast != null) { paint.setalpha(current_point_opacity / 2); paint.setcolor(resultpointcolor); float radius = point_size / 2.0f; for (resultpoint point : currentlast) { canvas.drawcircle(frameleft + (int) (point.getx() * scalex), frametop + (int) (point.gety() * scaley), radius, paint); } } postinvalidatedelayed(16, frame.left, frame.top, frame.right, frame.bottom); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。