Android实现扫码功能

Android实现扫码功能

2023年7月13日发(作者:)

Android实现扫码功能本⽂实例为⼤家分享了Android实现扫码功能的具体代码,供⼤家参考,具体内容如下1、引⼊implementation 'yapps:zxing-android-embedded:3.5.0'2、使⽤:public void initScan() { IntentIntegrator integrator = new IntentIntegrator(this); // 设置要扫描的条码类型,ONE_D_CODE_TYPES:⼀维码,QR_CODE_TYPES-⼆维码 iredBarcodeFormats(_CODE_TYPES); tureActivity(); //设置打开摄像头的Activity mpt(""); //底部的提⽰⽂字,设为""可以置空 eraId(0); //前置或者后置摄像头 pEnabled(true); //扫描成功的「哔哔」声,默认开启 codeImageEnabled(true); teScan(); }

@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { vityResult(requestCode, resultCode, intent); if (requestCode == T_CODE) { IntentResult scanResult = ctivityResult(requestCode, resultCode, intent); if (scanResult != null && tents() != null) { String result = tents(); LogUtil.d("扫码返回: " + result); try { JSONObject jsonObject = new JSONObject(result); if ((LICEMENID)) { //TODO 逻辑

} else { ortToast("未扫描到有效的信息"); } } catch (Exception e) { ortToast("未扫描到有效的信息"); tackTrace(); } } else { ortToast("未扫描到有效的信息"); } } }3、ScanActivitypublic class ScanActivity extends BackActivity {

@BindView() DecoratedBarcodeView mDbv; private CaptureManager captureManager;

@Override protected int getLayoutId() { getWindow().setFlags(_FULLSCREEN, _FULLSCREEN); return ty_scan; }

@Override protected void init() { (); captureManager = new CaptureManager(this, mDbv); lizeFromIntent(getIntent(), getSavedInstanceState()); (); }

@Override public void onSaveInstanceState(@NotNull Bundle outState, @NotNull PersistableBundle outPersistentState) { InstanceState(outState, outPersistentState); InstanceState(outState); }

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { return own(keyCode, event) || own(keyCode, event); }

@Override protected void onPause() { e(); e(); }

@Override public void onResume() { me(); me(); }

@Override protected void onDestroy() { roy(); roy(); }

}4、布局⽂件activity_scan

view_qr

< 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" />

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 = ources().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 = th(); int height = ght(); //绘制4个⾓

or(getResources().getColor(_white));//定义画笔的颜⾊ ct(, , + ScreenRate, + CORNER_WIDTH, paint); ct(, , + CORNER_WIDTH, + ScreenRate, paint);

ct( - ScreenRate, , , + CORNER_WIDTH, paint); ct( - CORNER_WIDTH, , , + ScreenRate, paint);

ct(, - CORNER_WIDTH, + ScreenRate, , paint); ct(, - ScreenRate, + CORNER_WIDTH, , paint);

ct( - ScreenRate, - CORNER_WIDTH, , , paint); ct( - CORNER_WIDTH, - ScreenRate, , , paint);

// 画出外部(即构图矩形之外)变暗 or(resultBitmap != null ? resultColor : maskColor); ct(0, 0, width, , paint); ct(0, , , , paint); ct(, , width, , paint); ct(0, , width, height, paint);

if (resultBitmap != null) { // Draw the opaque result bitmap over the scanning rectangle ha(CURRENT_POINT_OPACITY); tmap(resultBitmap, null, frame, paint); } else {

laserLinePosition = laserLinePosition + 8; if (laserLinePosition >= ()) { laserLinePosition = 0; } linearGradient = new LinearGradient( + 1, + laserLinePosition, - 1, + 10 + laserLinePosition, colors, position, ); // Draw a red "laser scanner" line through the middle to show decoding is active

der(linearGradient); //绘制扫描线 ct( + 1, + laserLinePosition, - 1, + 10 + laserLinePosition, paint); der(null); float scaleX = () / (float) (); float scaleY = () / (float) ();

List currentPossible = possibleResultPoints; List currentLast = lastPossibleResultPoints; int frameLeft = ; int frameTop = ; if (y()) { lastPossibleResultPoints = null; } else { possibleResultPoints = new ArrayList<>(5); lastPossibleResultPoints = currentPossible; ha(CURRENT_POINT_OPACITY); or(resultPointColor); for (ResultPoint point : currentPossible) { rcle(frameLeft + (int) (() * scaleX), frameTop + (int) (() * scaleY), POINT_SIZE, paint); } } if (currentLast != null) { ha(CURRENT_POINT_OPACITY / 2); or(resultPointColor); float radius = POINT_SIZE / 2.0f; for (ResultPoint point : currentLast) { rcle(frameLeft + (int) (() * scaleX), frameTop + (int) (() * scaleY), radius, paint); } } postInvalidateDelayed(16, , , , ); } }}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689214994a222127.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信