Search in sources :

Example 36 with ChecksumException

use of com.google.zxing.ChecksumException in project my-hardware by kellysong.

the class CaptureActivity method scanningImage.

/**
 * 图片识别
 *
 * @param path
 * @return
 */
public static Result scanningImage(String path) {
    if (TextUtils.isEmpty(path)) {
        return null;
    }
    // DecodeHintType 和EncodeHintType
    Hashtable<DecodeHintType, String> hints = new Hashtable<>();
    // 设置二维码内容的编码
    hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
    BitmapFactory.Options options = new BitmapFactory.Options();
    // options.inJustDecodeBounds = true; // 先获取原大小
    // 获取新的大小
    options.inJustDecodeBounds = false;
    int sampleSize = (int) (options.outHeight / (float) 200);
    if (sampleSize <= 0)
        sampleSize = 1;
    options.inSampleSize = sampleSize;
    Bitmap scanBitmap = BitmapFactory.decodeFile(path, options);
    int[] intArray = new int[scanBitmap.getWidth() * scanBitmap.getHeight()];
    scanBitmap.getPixels(intArray, 0, scanBitmap.getWidth(), 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight());
    RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), intArray);
    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
    Reader reader = new MultiFormatReader();
    try {
        return reader.decode(bitmap1, hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) ChecksumException(com.google.zxing.ChecksumException) MultiFormatReader(com.google.zxing.MultiFormatReader) Reader(com.google.zxing.Reader) NotFoundException(com.google.zxing.NotFoundException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) BitmapFactory(android.graphics.BitmapFactory) BinaryBitmap(com.google.zxing.BinaryBitmap) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource)

Example 37 with ChecksumException

use of com.google.zxing.ChecksumException in project MyBaseApplication by BanShouWeng.

the class AlbumDecoding method scanningImage.

/**
 * 扫描二维码图片的方法
 *
 * @param path 图片路径
 * @return 扫描结果
 */
private static Result scanningImage(String path) {
    if (TextUtils.isEmpty(path)) {
        return null;
    }
    Hashtable<DecodeHintType, String> hints = new Hashtable<>();
    // 设置二维码内容的编码
    hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
    BitmapFactory.Options options = new BitmapFactory.Options();
    // 先获取原大小
    options.inJustDecodeBounds = true;
    // 扫描图片的bitmap
    Bitmap scanBitmap = BitmapFactory.decodeFile(path, options);
    // 获取新的大小
    options.inJustDecodeBounds = false;
    int sampleSize = (int) (options.outHeight / (float) 200);
    if (sampleSize <= 0)
        sampleSize = 1;
    options.inSampleSize = sampleSize;
    scanBitmap = BitmapFactory.decodeFile(path, options);
    int[] intArray = new int[scanBitmap.getWidth() * scanBitmap.getHeight()];
    scanBitmap.getPixels(intArray, 0, scanBitmap.getWidth(), 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight());
    RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), intArray);
    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
    Result result = null;
    // 二维码解码
    QRCodeReader qrCodeReader = new QRCodeReader();
    try {
        result = qrCodeReader.decode(bitmap1, hints);
    } catch (NotFoundException | ChecksumException | FormatException e) {
        e.printStackTrace();
    }
    if (null == result) {
        // DataMatrix解码
        DataMatrixReader dataMatrixReader = new DataMatrixReader();
        try {
            result = dataMatrixReader.decode(bitmap1, hints);
        } catch (NotFoundException | ChecksumException | FormatException e) {
            e.printStackTrace();
        }
    }
    return result;
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) DataMatrixReader(com.google.zxing.datamatrix.DataMatrixReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) ChecksumException(com.google.zxing.ChecksumException) NotFoundException(com.google.zxing.NotFoundException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) BitmapFactory(android.graphics.BitmapFactory) BinaryBitmap(com.google.zxing.BinaryBitmap) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource)

Aggregations

ChecksumException (com.google.zxing.ChecksumException)37 FormatException (com.google.zxing.FormatException)31 BinaryBitmap (com.google.zxing.BinaryBitmap)22 NotFoundException (com.google.zxing.NotFoundException)22 HybridBinarizer (com.google.zxing.common.HybridBinarizer)19 RGBLuminanceSource (com.google.zxing.RGBLuminanceSource)14 Result (com.google.zxing.Result)14 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)14 DecodeHintType (com.google.zxing.DecodeHintType)13 Bitmap (android.graphics.Bitmap)11 Hashtable (java.util.Hashtable)10 BitmapFactory (android.graphics.BitmapFactory)8 IOException (java.io.IOException)7 LuminanceSource (com.google.zxing.LuminanceSource)6 Reader (com.google.zxing.Reader)6 DecoderResult (com.google.zxing.common.DecoderResult)6 MultiFormatReader (com.google.zxing.MultiFormatReader)5 ResultPoint (com.google.zxing.ResultPoint)4 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)4 Nullable (androidx.annotation.Nullable)3