Search in sources :

Example 26 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project ngtesting-platform by aaronchen2k.

the class QrcodeServiceImpl method decode.

@Override
public JSONObject decode(String filePath) {
    BufferedImage image;
    try {
        image = ImageIO.read(new File(filePath));
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
        Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result result = new MultiFormatReader().decode(binaryBitmap, hints);
        System.out.println(result.getText());
        JSONObject content = JSONObject.parseObject(result.getText());
        System.out.println("encode: " + result.getBarcodeFormat());
        return content;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) HashMap(java.util.HashMap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) JSONObject(com.alibaba.fastjson.JSONObject) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) JSONObject(com.alibaba.fastjson.JSONObject) BinaryBitmap(com.google.zxing.BinaryBitmap) File(java.io.File) Binarizer(com.google.zxing.Binarizer) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 27 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project zxing by yuzhiqiang1993.

the class DecodeImgThread method run.

@Override
public void run() {
    super.run();
    if (TextUtils.isEmpty(imgPath) || callback == null) {
        return;
    }
    /**
     * 对图片进行裁剪,防止oom
     */
    BitmapFactory.Options options = new BitmapFactory.Options();
    // 先获取原大小
    options.inJustDecodeBounds = true;
    scanBitmap = BitmapFactory.decodeFile(imgPath, options);
    // 获取新的大小
    options.inJustDecodeBounds = false;
    int sampleSize = (int) (options.outHeight / (float) 400);
    if (sampleSize <= 0)
        sampleSize = 1;
    options.inSampleSize = sampleSize;
    scanBitmap = BitmapFactory.decodeFile(imgPath, options);
    MultiFormatReader multiFormatReader = new MultiFormatReader();
    // 解码的参数
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2);
    // 可以解析的编码类型
    Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>();
    if (decodeFormats == null || decodeFormats.isEmpty()) {
        decodeFormats = new Vector<BarcodeFormat>();
        // 扫描的类型  一维码和二维码
        decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
        decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
        decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
    }
    hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
    // 设置解析的字符编码格式为UTF8
    hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
    // 设置解析配置参数
    multiFormatReader.setHints(hints);
    // 开始对图像资源解码
    Result rawResult = null;
    try {
        rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(scanBitmap))));
        Log.i("解析结果", rawResult.getText());
    } catch (Exception e) {
        e.printStackTrace();
    // Log.i("解析的图片结果","失败");
    }
    if (rawResult != null) {
        callback.onImageDecodeSuccess(rawResult);
    } else {
        callback.onImageDecodeFailed();
    }
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) BarcodeFormat(com.google.zxing.BarcodeFormat) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) BitmapFactory(android.graphics.BitmapFactory) BinaryBitmap(com.google.zxing.BinaryBitmap) Vector(java.util.Vector)

Example 28 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project wechat by dllwh.

the class QrcodeUtilHelper method decode.

/**
 * @方法描述: 解析二维码
 * @创建者: 独泪了无痕
 * @创建时间: 2016年2月26日 下午6:03:54
 * @param imgPath
 *            二维码路径
 * @return 返回解析后的内容
 */
public static String decode(String imgPath) {
    if (StringUtils.isBlank(imgPath)) {
        System.out.println("尚未找到想要解析的图片");
        return "";
    }
    BufferedImage image;
    Result result;
    try {
        image = ImageIO.read(new File(imgPath));
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        // 解码设置编码方式为:GBK
        hints.put(DecodeHintType.CHARACTER_SET, "GBK");
        result = new MultiFormatReader().decode(bitmap, hints);
        // 对图像进行解码
        String resultStr = result.getText();
        return resultStr;
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (NotFoundException nfe) {
        nfe.printStackTrace();
    }
    return "";
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) File(java.io.File)

Example 29 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project android-app by eoecn.

the class DecodeHandler method decode.

/**
 * Decode the data within the viewfinder rectangle, and time how long it
 * took. For efficiency, reuse the same reader objects from one decode to
 * the next.
 *
 * @param data The YUV preview frame.
 * @param width The width of the preview frame.
 * @param height The height of the preview frame.
 */
private void decode(byte[] data, int width, int height) {
    long start = System.currentTimeMillis();
    Result rawResult = null;
    // modify here
    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    // Here we are swapping, that's the difference to #11
    int tmp = width;
    width = height;
    height = tmp;
    PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    try {
        rawResult = multiFormatReader.decodeWithState(bitmap);
    } catch (ReaderException re) {
    // continue
    } finally {
        multiFormatReader.reset();
    }
    if (rawResult != null) {
        long end = System.currentTimeMillis();
        Log.d(TAG, "Found barcode (" + (end - start) + " ms):\n" + rawResult.toString());
        Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);
        Bundle bundle = new Bundle();
        bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());
        message.setData(bundle);
        // Log.d(TAG, "Sending decode succeeded message...");
        message.sendToTarget();
    } else {
        Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
        message.sendToTarget();
    }
}
Also used : Message(android.os.Message) PlanarYUVLuminanceSource(com.google.zxing.camera.PlanarYUVLuminanceSource) Bundle(android.os.Bundle) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 30 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class QrCameraTest method testDecode_unicodePictureCaptured_QrCodeCorrectValue.

@Test
public void testDecode_unicodePictureCaptured_QrCodeCorrectValue() {
    final String unicodeTest = "中文測試";
    try {
        final Bitmap bmp = QrCodeGenerator.encodeQrCode(unicodeTest, 320);
        final int[] intArray = new int[bmp.getWidth() * bmp.getHeight()];
        bmp.getPixels(intArray, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
        LuminanceSource source = new RGBLuminanceSource(bmp.getWidth(), bmp.getHeight(), intArray);
        final BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        mCamera.decodeImage(bitmap);
        bmp.recycle();
    } catch (WriterException e) {
    }
    assertThat(mQrCode).isEqualTo(unicodeTest);
}
Also used : Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) LuminanceSource(com.google.zxing.LuminanceSource) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource) WriterException(com.google.zxing.WriterException) Test(org.junit.Test)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)78 Result (com.google.zxing.Result)62 HybridBinarizer (com.google.zxing.common.HybridBinarizer)61 ReaderException (com.google.zxing.ReaderException)39 Message (android.os.Message)24 Bundle (android.os.Bundle)23 BufferedImage (java.awt.image.BufferedImage)21 MultiFormatReader (com.google.zxing.MultiFormatReader)20 NotFoundException (com.google.zxing.NotFoundException)20 LuminanceSource (com.google.zxing.LuminanceSource)19 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)19 DecodeHintType (com.google.zxing.DecodeHintType)15 Handler (android.os.Handler)14 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)13 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)11 Hashtable (java.util.Hashtable)11 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)10 Test (org.junit.Test)10 BitArray (com.google.zxing.common.BitArray)8 Bitmap (android.graphics.Bitmap)7