电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android實現掃碼功能

瀏覽:6日期:2022-09-18 11:57:08

本文實例為大家分享了Android實現掃碼功能的具體代碼,供大家參考,具體內容如下

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); //設置打開攝像頭的Activityintegrator.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.DecoratedBarcodeViewandroid: 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.BarcodeViewandroid: 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.QrViewandroid: 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' /> <TextViewandroid: 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.MyActionBarandroid: 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);} }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 哔咔漫画网页版在线_下载入口访问指引 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 西安耀程造价培训机构_工程预算实训_广联达实作实操培训 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 球盟会·(中国)官方网站 | 烽火安全网_加密软件、神盾软件官网 | 税筹星_灵活用工平台_企业财务顾问_财税法薪综合服务平台 | 千淘酒店差旅平台-中国第一家针对TMC行业的酒店资源供应平台 | 利浦顿蒸汽发生器厂家-电蒸汽发生器/燃气蒸汽发生器_湖北利浦顿热能科技有限公司官网 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | 定制/定做冲锋衣厂家/公司-订做/订制冲锋衣价格/费用-北京圣达信 | 灰板纸、灰底白、硬纸板等纸品生产商-金泊纸业 | 铝扣板-铝方通-铝格栅-铝条扣板-铝单板幕墙-佳得利吊顶天花厂家 elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 搜活动房网—活动房_集装箱活动房_集成房屋_活动房屋 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 包装设计公司,产品包装设计|包装制作,包装盒定制厂家-汇包装【官方网站】 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 企业微信营销_企业微信服务商_私域流量运营_艾客SCRM官网 | LOGO设计_品牌设计_VI设计 - 特创易 | 贴片电容-贴片电阻-二三极管-国巨|三星|风华贴片电容代理商-深圳伟哲电子 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 超声波乳化机-超声波分散机|仪-超声波萃取仪-超声波均质机-精浩机械|首页 | 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 | 公交驾校-北京公交驾校欢迎您! 工作心得_读书心得_学习心得_找心得体会范文就上学道文库 | 南京泽朗生物科技有限公司-液体饮料代加工_果汁饮料代加工_固体饮料代加工 | 我爱古诗词_古诗词名句赏析学习平台 | 合肥触摸一体机_触摸查询机厂家_合肥拼接屏-安徽迅博智能科技 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 电动葫芦-河北悍象起重机械有限公司 | 聚氨酯催化剂K15,延迟催化剂SA-1,叔胺延迟催化剂,DBU,二甲基哌嗪,催化剂TMR-2,-聚氨酯催化剂生产厂家 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | TYPE-C厂家|TYPE-C接口|TYPE-C防水母座|TYPE-C贴片-深圳步步精 | 旋片真空泵_真空泵_水环真空泵_真空机组-深圳恒才机电设备有限公司 | 成都竞价托管_抖音代运营_网站建设_成都SEM外包-成都智网创联网络科技有限公司 | 健康管理师报考条件,考试时间,报名入口—首页 | Maneurop/美优乐压缩机,活塞压缩机,型号规格,技术参数,尺寸图片,价格经销商 | 青岛成人高考_山东成考报名网 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 |