From 5e8ea12f691b3ae6d8a1b138674129c703e6e76f Mon Sep 17 00:00:00 2001 From: fengjingqi Date: Wed, 11 Nov 2026 14:23:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=AB=E7=A0=81=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E5=88=B0=20ZXingLite=20=E5=B9=B6=E6=94=AF=E6=8C=81=E7=9B=B8?= =?UTF-8?q?=E5=86=8C=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 8 +- app/src/main/AndroidManifest.xml | 12 + .../griddlebiz/dialog/ShareQrcodeDialog.java | 8 +- .../product/ProductInboundActivity.java | 53 ++-- .../product/ProductScanActivity.java | 300 ++++++++++++++++++ .../res/drawable/ic_product_scan_back.xml | 10 + .../drawable/ic_product_scan_flash_off.xml | 10 + .../res/drawable/ic_product_scan_flash_on.xml | 10 + .../res/drawable/ic_product_scan_gallery.xml | 10 + .../drawable/product_scan_bottom_scrim.xml | 8 + .../drawable/product_scan_flash_selector.xml | 5 + .../res/drawable/product_scan_gallery_bg.xml | 6 + .../res/drawable/product_scan_top_scrim.xml | 8 + .../main/res/layout/product_scan_layout.xml | 107 +++++++ .../main/res/values-en/product_strings.xml | 9 +- app/src/main/res/values/product_strings.xml | 9 +- app/src/main/res/values/product_styles.xml | 10 + 17 files changed, 544 insertions(+), 39 deletions(-) create mode 100644 app/src/main/java/com/ckkj/griddlebiz/product/ProductScanActivity.java create mode 100644 app/src/main/res/drawable/ic_product_scan_back.xml create mode 100644 app/src/main/res/drawable/ic_product_scan_flash_off.xml create mode 100644 app/src/main/res/drawable/ic_product_scan_flash_on.xml create mode 100644 app/src/main/res/drawable/ic_product_scan_gallery.xml create mode 100644 app/src/main/res/drawable/product_scan_bottom_scrim.xml create mode 100644 app/src/main/res/drawable/product_scan_flash_selector.xml create mode 100644 app/src/main/res/drawable/product_scan_gallery_bg.xml create mode 100644 app/src/main/res/drawable/product_scan_top_scrim.xml create mode 100644 app/src/main/res/layout/product_scan_layout.xml diff --git a/app/build.gradle b/app/build.gradle index 55b7e43..bf9eafa 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -156,10 +156,10 @@ dependencies { implementation 'com.geyifeng.immersionbar:immersionbar:3.2.2' - //zxing 依赖 二维码,扫描等使用 - implementation 'com.google.zxing:core:3.4.1' -// implementation 'com.journeyapps:zxing-android-embedded:4.3.0' - implementation 'com.github.maning0303:MNZXingCode:V2.1.9' + //扫码:ZXingLite(CameraX + zxing-core),替代已停止维护的 MNZXingCode + implementation 'com.github.jenly1314:zxing-lite:3.5.0' + //相册选择:AndroidX Photo Picker,无需存储权限 + implementation 'androidx.activity:activity:1.9.3' //该库专门用于在 ImageView 中处理 GIF 动画。 // implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.23' //下载框架 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f150f77..e3b0f31 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,13 @@ + + + @@ -151,6 +158,11 @@ android:name=".product.ProductInboundActivity" android:screenOrientation="portrait" android:windowSoftInputMode="adjustResize" /> + mScanLauncher = registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), result -> { + if (result.getResultCode() != RESULT_OK) { + return; + } + Intent data = result.getData(); + String code = data == null ? null : data.getStringExtra(CameraScan.SCAN_RESULT); + if (TextUtils.isEmpty(code)) { + ToastUtil.showShort(mContext, R.string.product_scan_empty); + } else { + mPresenter.submitCode(code); + } + }); + private Dialog mManualCodeDialog; private DialogProductInboundManualLayoutBinding mManualBinding; @@ -66,6 +83,11 @@ public class ProductInboundActivity extends BaseActivity + * 相册使用 AndroidX Photo Picker,不需要读取存储权限;返回结果统一放在 + * {@link CameraScan#SCAN_RESULT} 中。 + */ +public class ProductScanActivity extends BarcodeCameraScanActivity { + + private final Handler mMainHandler = new Handler(Looper.getMainLooper()); + private final ExecutorService mDecodeExecutor = Executors.newSingleThreadExecutor(); + + private TextView mTvGallery; + private boolean mDecoding; + private boolean mResultReturned; + + private final ActivityResultLauncher mPickImageLauncher = + registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), this::onImagePicked); + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + applyEdgeToEdge(); + findViewById(R.id.ivScanBack).setOnClickListener(v -> finish()); + mTvGallery = findViewById(R.id.tvGallery); + mTvGallery.setOnClickListener(v -> openPhotoPicker()); + } + + private void applyEdgeToEdge() { + Window window = getWindow(); + WindowCompat.setDecorFitsSystemWindows(window, false); + window.setStatusBarColor(Color.TRANSPARENT); + window.setNavigationBarColor(Color.BLACK); + WindowCompat.getInsetsController(window, window.getDecorView()) + .setAppearanceLightStatusBars(false); + + View root = findViewById(R.id.scanRoot); + View topBar = findViewById(R.id.scanTopBar); + View bottomBar = findViewById(R.id.scanBottomBar); + final int topPadding = topBar.getPaddingTop(); + final int bottomPadding = bottomBar.getPaddingBottom(); + ViewCompat.setOnApplyWindowInsetsListener(root, (v, insets) -> { + Insets bars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + topBar.setPadding(topBar.getPaddingLeft(), topPadding + bars.top, + topBar.getPaddingRight(), topBar.getPaddingBottom()); + bottomBar.setPadding(bottomBar.getPaddingLeft(), bottomBar.getPaddingTop(), + bottomBar.getPaddingRight(), bottomPadding + bars.bottom); + return insets; + }); + ViewCompat.requestApplyInsets(root); + } + + @Override + public int getLayoutId() { + return R.layout.product_scan_layout; + } + + @Override + public Analyzer createAnalyzer() { + // 默认支持 QR_CODE / UPC_A / EAN_13 / CODE_128,覆盖设备外箱常见条码。 + return new MultiFormatAnalyzer(new DecodeConfig().setFullAreaScan(true)); + } + + @Override + public void initCameraScan(@NonNull CameraScan cameraScan) { + super.initCameraScan(cameraScan); + cameraScan.setPlayBeep(true) + .setVibrate(true) + .setAnalyzeImage(true); + } + + @Override + public void onScanResultCallback(@NonNull AnalyzeResult result) { + Result value = result.getResult(); + if (value == null || TextUtils.isEmpty(value.getText())) { + return; + } + CameraScan cameraScan = getCameraScan(); + if (cameraScan != null) { + cameraScan.setAnalyzeImage(false); + } + returnResult(value.getText()); + } + + private void openPhotoPicker() { + if (mDecoding) { + return; + } + CameraScan cameraScan = getCameraScan(); + if (cameraScan != null) { + // 系统相册打开期间暂停识别,避免相机结果提前返回。 + cameraScan.setAnalyzeImage(false); + } + mPickImageLauncher.launch(new PickVisualMediaRequest.Builder() + .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE) + .build()); + } + + private void onImagePicked(@Nullable Uri uri) { + if (uri == null) { + // 用户取消选择,恢复相机识别。 + resumeScan(); + return; + } + setDecoding(true); + mDecodeExecutor.execute(() -> { + String text = decodeImage(uri); + mMainHandler.post(() -> { + setDecoding(false); + if (TextUtils.isEmpty(text)) { + ToastUtil.showShort(this, R.string.product_scan_gallery_failed); + resumeScan(); + } else { + returnResult(text); + } + }); + }); + } + + @Nullable + private String decodeImage(Uri uri) { + Bitmap bitmap = null; + try { + bitmap = loadSampledBitmap(uri); + if (bitmap == null) { + return null; + } + bitmap = applyExifRotation(uri, bitmap); + String text = CodeUtils.parseCode(bitmap); + if (!TextUtils.isEmpty(text)) { + return text; + } + // 相册图片可能本身带旋转角度,逐个方向重试。 + for (int degrees = 90; degrees < 360; degrees += 90) { + Bitmap rotated = rotate(bitmap, degrees); + if (rotated == null) { + continue; + } + text = CodeUtils.parseCode(rotated); + if (rotated != bitmap) { + rotated.recycle(); + } + if (!TextUtils.isEmpty(text)) { + return text; + } + } + return null; + } catch (Exception e) { + return null; + } finally { + if (bitmap != null && !bitmap.isRecycled()) { + bitmap.recycle(); + } + } + } + + @Nullable + private Bitmap loadSampledBitmap(Uri uri) throws Exception { + BitmapFactory.Options bounds = new BitmapFactory.Options(); + bounds.inJustDecodeBounds = true; + try (InputStream in = getContentResolver().openInputStream(uri)) { + if (in == null) { + return null; + } + BitmapFactory.decodeStream(in, null, bounds); + } + int sampleSize = 1; + int maxSize = Math.max(bounds.outWidth, bounds.outHeight); + if (maxSize <= 0) { + return null; + } + while (maxSize / sampleSize > 1600) { + sampleSize *= 2; + } + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inSampleSize = sampleSize; + try (InputStream in = getContentResolver().openInputStream(uri)) { + if (in == null) { + return null; + } + return BitmapFactory.decodeStream(in, null, options); + } + } + + private Bitmap applyExifRotation(Uri uri, Bitmap bitmap) { + try (InputStream in = getContentResolver().openInputStream(uri)) { + if (in == null) { + return bitmap; + } + ExifInterface exif = new ExifInterface(in); + int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, + ExifInterface.ORIENTATION_NORMAL); + int degrees; + switch (orientation) { + case ExifInterface.ORIENTATION_ROTATE_90: + degrees = 90; + break; + case ExifInterface.ORIENTATION_ROTATE_180: + degrees = 180; + break; + case ExifInterface.ORIENTATION_ROTATE_270: + degrees = 270; + break; + default: + return bitmap; + } + Bitmap rotated = rotate(bitmap, degrees); + return rotated == null ? bitmap : rotated; + } catch (Exception e) { + return bitmap; + } + } + + @Nullable + private Bitmap rotate(Bitmap bitmap, int degrees) { + if (degrees % 360 == 0) { + return bitmap; + } + try { + Matrix matrix = new Matrix(); + matrix.postRotate(degrees); + return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), + matrix, true); + } catch (Exception e) { + return null; + } + } + + private void setDecoding(boolean decoding) { + mDecoding = decoding; + mTvGallery.setEnabled(!decoding); + mTvGallery.setText(decoding ? R.string.product_scan_decoding : R.string.product_scan_gallery); + } + + private void resumeScan() { + CameraScan cameraScan = getCameraScan(); + if (cameraScan != null) { + cameraScan.setAnalyzeImage(true); + } + } + + private void returnResult(String text) { + if (mResultReturned) { + return; + } + mResultReturned = true; + Intent data = new Intent(); + data.putExtra(CameraScan.SCAN_RESULT, text); + setResult(RESULT_OK, data); + finish(); + } + + @Override + protected void onDestroy() { + mDecodeExecutor.shutdownNow(); + mMainHandler.removeCallbacksAndMessages(null); + super.onDestroy(); + } +} diff --git a/app/src/main/res/drawable/ic_product_scan_back.xml b/app/src/main/res/drawable/ic_product_scan_back.xml new file mode 100644 index 0000000..22c5a36 --- /dev/null +++ b/app/src/main/res/drawable/ic_product_scan_back.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/drawable/ic_product_scan_flash_off.xml b/app/src/main/res/drawable/ic_product_scan_flash_off.xml new file mode 100644 index 0000000..f789372 --- /dev/null +++ b/app/src/main/res/drawable/ic_product_scan_flash_off.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/drawable/ic_product_scan_flash_on.xml b/app/src/main/res/drawable/ic_product_scan_flash_on.xml new file mode 100644 index 0000000..da21698 --- /dev/null +++ b/app/src/main/res/drawable/ic_product_scan_flash_on.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/drawable/ic_product_scan_gallery.xml b/app/src/main/res/drawable/ic_product_scan_gallery.xml new file mode 100644 index 0000000..84b409e --- /dev/null +++ b/app/src/main/res/drawable/ic_product_scan_gallery.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/drawable/product_scan_bottom_scrim.xml b/app/src/main/res/drawable/product_scan_bottom_scrim.xml new file mode 100644 index 0000000..38cb8aa --- /dev/null +++ b/app/src/main/res/drawable/product_scan_bottom_scrim.xml @@ -0,0 +1,8 @@ + + + + diff --git a/app/src/main/res/drawable/product_scan_flash_selector.xml b/app/src/main/res/drawable/product_scan_flash_selector.xml new file mode 100644 index 0000000..7530f4e --- /dev/null +++ b/app/src/main/res/drawable/product_scan_flash_selector.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/product_scan_gallery_bg.xml b/app/src/main/res/drawable/product_scan_gallery_bg.xml new file mode 100644 index 0000000..b21d037 --- /dev/null +++ b/app/src/main/res/drawable/product_scan_gallery_bg.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/app/src/main/res/drawable/product_scan_top_scrim.xml b/app/src/main/res/drawable/product_scan_top_scrim.xml new file mode 100644 index 0000000..0a5ee50 --- /dev/null +++ b/app/src/main/res/drawable/product_scan_top_scrim.xml @@ -0,0 +1,8 @@ + + + + diff --git a/app/src/main/res/layout/product_scan_layout.xml b/app/src/main/res/layout/product_scan_layout.xml new file mode 100644 index 0000000..279d497 --- /dev/null +++ b/app/src/main/res/layout/product_scan_layout.xml @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values-en/product_strings.xml b/app/src/main/res/values-en/product_strings.xml index 411fb1f..08fd492 100644 --- a/app/src/main/res/values-en/product_strings.xml +++ b/app/src/main/res/values-en/product_strings.xml @@ -11,10 +11,15 @@ Close Please enter the QR code number Code submitted: %1$s - Scanner is not connected yet Camera Permission Product Inbound uses the camera to scan the device outer box barcode. If you decline, you can still enter the SN code manually. Camera permission was not granted. You can enter the SN code manually. - Scan failed: %1$s + No camera is available on this device. Please enter the SN code manually. No barcode was detected. Please try again. + Scan to Inbound + Choose from Gallery + Decoding image… + Align the barcode within the frame to scan + No barcode was found in the image. + Flashlight diff --git a/app/src/main/res/values/product_strings.xml b/app/src/main/res/values/product_strings.xml index 953b401..bb3476c 100644 --- a/app/src/main/res/values/product_strings.xml +++ b/app/src/main/res/values/product_strings.xml @@ -11,10 +11,15 @@ 关闭 请输入二维码编号 已提交编码:%1$s - 扫码能力待接入 相机权限使用说明 商品入库需要使用相机扫描设备外箱条码。拒绝或取消授权不影响手动输入 SN 码。 未获得相机权限,可手动输入 SN 码 - 扫码失败:%1$s + 当前设备没有可用相机,请手动输入 SN 码 未识别到条码,请重试 + 扫码入库 + 从相册选择 + 正在识别图片… + 将条码放入框内,即可自动识别 + 未从图片中识别到条码 + 手电筒 diff --git a/app/src/main/res/values/product_styles.xml b/app/src/main/res/values/product_styles.xml index 3596ffa..abce861 100644 --- a/app/src/main/res/values/product_styles.xml +++ b/app/src/main/res/values/product_styles.xml @@ -10,4 +10,14 @@ 0.6 true + + +