Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么有的二维码可以识别,有的会出错 #6

Closed
helloshuang opened this issue Dec 29, 2018 · 12 comments
Closed

为什么有的二维码可以识别,有的会出错 #6

helloshuang opened this issue Dec 29, 2018 · 12 comments

Comments

@helloshuang
Copy link

No description provided.

@LewinJun
Copy link
Owner

LewinJun commented Jan 2, 2019

安卓还是iOS?

@helloshuang
Copy link
Author

都有这个情况,ios一部分真机可以扫,模拟器不行,有的真机也不行,安卓我试了几个真机都不行

@LewinJun
Copy link
Owner

麻烦你贴几个二维码给我,我试下,谢谢

@LewinJun
Copy link
Owner

还有就是你用的哪些机型?

@helloshuang
Copy link
Author

6d345abe-1cf8-4618-a50f-c560a9209150
安卓的大多都不行,ios模拟器不行,真机可以

@noraliucode
Copy link

請問目前安卓的掃描不支援嗎?

@LewinJun
Copy link
Owner

微信群 加入群聊及时通知

@hi-zd
Copy link

hi-zd commented Apr 17, 2020

我也碰到类似问题,安卓有些图片无法识别

@LewinJun
Copy link
Owner

最近抽时间 优化下, 最近项目太忙了,pc, mobile, app(安卓 iOS) ,小程序, 大前端要做的事太多了

@hi-zd
Copy link

hi-zd commented Apr 27, 2020

最近抽时间优化下,最近项目太忙了,pc,mobile,app(安卓iOS),小程序,大前端要做的事太多了

/**
* 扫描二维码图片的方法
*
* @param path
* @return
*/
` public Result scanningImage(String path) {
if (path == null || path.length() == 0) {
return null;
}
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); // 设置二维码内容的编码
hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);// 编码格式
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);// 花更多的时间用于寻找图上的编码,优化准确性,但不优化速度,Boolean类型
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 先获取原大小
Bitmap scanBitmap = BitmapFactory.decodeFile(path);
options.inJustDecodeBounds = false; // 获取新的大小
int sampleSize = (int) (options.outHeight / (float) 200);
// 添加以下代码
int w = options.outWidth;
int h = options.outHeight;
// 获取手机屏幕分辨率
DisplayMetrics dm = new DisplayMetrics();
getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
// // 计算真实图片与屏幕分辨率的比率w
int widthSample = w / screenWidth;
int heightSample = h / screenHeight;
if (w > h && w > screenWidth) {// 如果宽度大的话根据宽度固定大小缩放
sampleSize = (int) (options.outWidth / screenWidth);
} else if (w < h && h > screenHeight) {// 如果高度高的话根据宽度固定大小缩放
sampleSize = (int) (options.outHeight / screenHeight);
}
if (sampleSize <= 0)
sampleSize = 1;

    options.inSampleSize = sampleSize;

    scanBitmap = BitmapFactory.decodeFile(path, options);
    int width = scanBitmap.getWidth();
    int height = scanBitmap.getHeight();
    int[] pixels = new int[width * height];
    scanBitmap.getPixels(pixels, 0, width, 0, 0, width, height);// 获取图片像素点
    RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), pixels);
    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    try {
        return reader.decode(bitmap1, hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }
    return null;
}`

这是我对scanningImage的一些修改,在我的项目里能有一些优化,如果不需要兼容安卓7以下的话建议用compile 'com.google.zxing:core:3.4.0',效果还不错

@LewinJun
Copy link
Owner

最近抽时间优化下,最近项目太忙了,pc,mobile,app(安卓iOS),小程序,大前端要做的事太多了

/**

  • 扫描二维码图片的方法
  • @param path
  • @return
    */
    ` public Result scanningImage(String path) {
    if (path == null || path.length() == 0) {
    return null;
    }
    Map<DecodeHintType, Object> hints = new HashMap<>();
    hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); // 设置二维码内容的编码
    hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);// 编码格式
    hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);// 花更多的时间用于寻找图上的编码,优化准确性,但不优化速度,Boolean类型
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true; // 先获取原大小
    Bitmap scanBitmap = BitmapFactory.decodeFile(path);
    options.inJustDecodeBounds = false; // 获取新的大小
    int sampleSize = (int) (options.outHeight / (float) 200);
    // 添加以下代码
    int w = options.outWidth;
    int h = options.outHeight;
    // 获取手机屏幕分辨率
    DisplayMetrics dm = new DisplayMetrics();
    getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenWidth = dm.widthPixels;
    int screenHeight = dm.heightPixels;
    // // 计算真实图片与屏幕分辨率的比率w
    int widthSample = w / screenWidth;
    int heightSample = h / screenHeight;
    if (w > h && w > screenWidth) {// 如果宽度大的话根据宽度固定大小缩放
    sampleSize = (int) (options.outWidth / screenWidth);
    } else if (w < h && h > screenHeight) {// 如果高度高的话根据宽度固定大小缩放
    sampleSize = (int) (options.outHeight / screenHeight);
    }
    if (sampleSize <= 0)
    sampleSize = 1;
    options.inSampleSize = sampleSize;

    scanBitmap = BitmapFactory.decodeFile(path, options);
    int width = scanBitmap.getWidth();
    int height = scanBitmap.getHeight();
    int[] pixels = new int[width * height];
    scanBitmap.getPixels(pixels, 0, width, 0, 0, width, height);// 获取图片像素点
    RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), pixels);
    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    try {
        return reader.decode(bitmap1, hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }
    return null;
}`

这是我对scanningImage的一些修改,在我的项目里能有一些优化,如果不需要兼容安卓7以下的话建议用compile 'com.google.zxing:core:3.4.0',效果还不错

谢谢,最近我会上一个版本,结合zxing和zbar两者使用,如果其中一个没识别会尝试使用另外一种

@LewinJun
Copy link
Owner

上面的最近抽空优化了,新版1.1可以识别上面的二维码了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants