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
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
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689214994a222127.html
评论列表(0条)