feat: 扫码迁移到 ZXingLite 并支持相册识别
This commit is contained in:
@@ -32,7 +32,7 @@ import androidx.viewpager.widget.ViewPager;
|
||||
import androidx.viewpager2.widget.ViewPager2;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.google.zxing.client.android.utils.ZXingUtils;
|
||||
import com.king.zxing.util.CodeUtils;
|
||||
import com.ckkj.griddlebiz.AppConfig;
|
||||
import com.ckkj.griddlebiz.R;
|
||||
import com.ckkj.griddlebiz.guide.GuideActivity;
|
||||
@@ -181,8 +181,10 @@ public class ShareQrcodeDialog {
|
||||
*/
|
||||
private void authUrlToQrcode(String data) {
|
||||
try {
|
||||
Bitmap qrImage = ZXingUtils.createQRCodeImage(AppConfig.USER_SHARE_URL + data);
|
||||
imgQrcode.setImageBitmap(qrImage);
|
||||
Bitmap qrImage = CodeUtils.createQRCode(AppConfig.USER_SHARE_URL + data, 600);
|
||||
if (qrImage != null) {
|
||||
imgQrcode.setImageBitmap(qrImage);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.ckkj.griddlebiz.product;
|
||||
import android.Manifest;
|
||||
import android.app.Dialog;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Bundle;
|
||||
@@ -13,10 +14,11 @@ import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.zxing.client.android.MNScanManager;
|
||||
import com.google.zxing.client.android.other.MNScanCallback;
|
||||
import com.king.camera.scan.CameraScan;
|
||||
import com.ckkj.griddlebiz.R;
|
||||
import com.ckkj.griddlebiz.base.BaseActivity;
|
||||
import com.ckkj.griddlebiz.databinding.DialogProductInboundManualLayoutBinding;
|
||||
@@ -39,6 +41,21 @@ public class ProductInboundActivity extends BaseActivity<ProductInboundLayoutBin
|
||||
|
||||
private static final int REQUEST_CAMERA_SCAN = 0x140;
|
||||
|
||||
/** 扫码页返回结果;扫码和相册识别都通过 {@link CameraScan#SCAN_RESULT} 回传。 */
|
||||
private final ActivityResultLauncher<Intent> 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<ProductInboundLayoutBin
|
||||
}
|
||||
|
||||
private void requestScanPermission() {
|
||||
// 相机在清单中声明为非必需硬件,无相机设备仍可安装和使用手动输入。
|
||||
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
|
||||
ToastUtil.showShort(mContext, R.string.product_camera_unavailable);
|
||||
return;
|
||||
}
|
||||
if (hasRecordPermission(Manifest.permission.CAMERA)) {
|
||||
startScan();
|
||||
return;
|
||||
@@ -78,32 +100,7 @@ public class ProductInboundActivity extends BaseActivity<ProductInboundLayoutBin
|
||||
}
|
||||
|
||||
private void startScan() {
|
||||
MNScanManager.startScan(this, new MNScanCallback() {
|
||||
@Override
|
||||
public void onActivityResult(int resultCode, Intent data) {
|
||||
switch (resultCode) {
|
||||
case MNScanManager.RESULT_SUCCESS:
|
||||
String success = data == null ? ""
|
||||
: data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_SUCCESS);
|
||||
if (TextUtils.isEmpty(success)) {
|
||||
ToastUtil.showShort(mContext, R.string.product_scan_empty);
|
||||
} else {
|
||||
mPresenter.submitCode(success);
|
||||
}
|
||||
break;
|
||||
case MNScanManager.RESULT_FAIL:
|
||||
String error = data == null ? ""
|
||||
: data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_ERROR);
|
||||
ToastUtil.showShort(mContext,
|
||||
getString(R.string.product_scan_failed,
|
||||
TextUtils.isEmpty(error) ? "" : error));
|
||||
break;
|
||||
case MNScanManager.RESULT_CANCLE:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
mScanLauncher.launch(new Intent(this, ProductScanActivity.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
package com.ckkj.griddlebiz.product;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Matrix;
|
||||
import android.media.ExifInterface;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.PickVisualMediaRequest;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
import com.ckkj.griddlebiz.R;
|
||||
import com.ckkj.griddlebiz.utils.show.ToastUtil;
|
||||
import com.google.zxing.Result;
|
||||
import com.king.camera.scan.AnalyzeResult;
|
||||
import com.king.camera.scan.CameraScan;
|
||||
import com.king.camera.scan.analyze.Analyzer;
|
||||
import com.king.zxing.BarcodeCameraScanActivity;
|
||||
import com.king.zxing.DecodeConfig;
|
||||
import com.king.zxing.analyze.MultiFormatAnalyzer;
|
||||
import com.king.zxing.util.CodeUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* 商品入库扫码页:相机实时扫码,也可从相册选择图片识别。
|
||||
* <p>
|
||||
* 相册使用 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<PickVisualMediaRequest> 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<Result> createAnalyzer() {
|
||||
// 默认支持 QR_CODE / UPC_A / EAN_13 / CODE_128,覆盖设备外箱常见条码。
|
||||
return new MultiFormatAnalyzer(new DecodeConfig().setFullAreaScan(true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initCameraScan(@NonNull CameraScan<Result> cameraScan) {
|
||||
super.initCameraScan(cameraScan);
|
||||
cameraScan.setPlayBeep(true)
|
||||
.setVibrate(true)
|
||||
.setAnalyzeImage(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanResultCallback(@NonNull AnalyzeResult<Result> result) {
|
||||
Result value = result.getResult();
|
||||
if (value == null || TextUtils.isEmpty(value.getText())) {
|
||||
return;
|
||||
}
|
||||
CameraScan<Result> cameraScan = getCameraScan();
|
||||
if (cameraScan != null) {
|
||||
cameraScan.setAnalyzeImage(false);
|
||||
}
|
||||
returnResult(value.getText());
|
||||
}
|
||||
|
||||
private void openPhotoPicker() {
|
||||
if (mDecoding) {
|
||||
return;
|
||||
}
|
||||
CameraScan<Result> 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<Result> 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user