Search in sources :

Example 1 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project zxing by zxing.

the class DecodeServlet method processImage.

private static void processImage(BufferedImage image, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
    Collection<Result> results = new ArrayList<>(1);
    try {
        Reader reader = new MultiFormatReader();
        ReaderException savedException = null;
        try {
            // Look for multiple barcodes
            MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
            Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
            if (theResults != null) {
                results.addAll(Arrays.asList(theResults));
            }
        } catch (ReaderException re) {
            savedException = re;
        }
        if (results.isEmpty()) {
            try {
                // Look for pure barcode
                Result theResult = reader.decode(bitmap, HINTS_PURE);
                if (theResult != null) {
                    results.add(theResult);
                }
            } catch (ReaderException re) {
                savedException = re;
            }
        }
        if (results.isEmpty()) {
            try {
                // Look for normal barcode in photo
                Result theResult = reader.decode(bitmap, HINTS);
                if (theResult != null) {
                    results.add(theResult);
                }
            } catch (ReaderException re) {
                savedException = re;
            }
        }
        if (results.isEmpty()) {
            try {
                // Try again with other binarizer
                BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
                Result theResult = reader.decode(hybridBitmap, HINTS);
                if (theResult != null) {
                    results.add(theResult);
                }
            } catch (ReaderException re) {
                savedException = re;
            }
        }
        if (results.isEmpty()) {
            try {
                throw savedException == null ? NotFoundException.getNotFoundInstance() : savedException;
            } catch (FormatException | ChecksumException e) {
                log.info(e.toString());
                errorResponse(request, response, "format");
            } catch (ReaderException e) {
                // Including NotFoundException
                log.info(e.toString());
                errorResponse(request, response, "notfound");
            }
            return;
        }
    } catch (RuntimeException re) {
        // Call out unexpected errors in the log clearly
        log.log(Level.WARNING, "Unexpected exception from library", re);
        throw new ServletException(re);
    }
    String fullParameter = request.getParameter("full");
    boolean minimalOutput = fullParameter != null && !Boolean.parseBoolean(fullParameter);
    if (minimalOutput) {
        response.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString());
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try (Writer out = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8)) {
            for (Result result : results) {
                out.write(result.getText());
                out.write('\n');
            }
        }
    } else {
        request.setAttribute("results", results);
        request.getRequestDispatcher("decoderesult.jspx").forward(request, response);
    }
}
Also used : GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) MultiFormatReader(com.google.zxing.MultiFormatReader) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) ChecksumException(com.google.zxing.ChecksumException) ArrayList(java.util.ArrayList) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) ImageReader(com.google.zxing.client.j2se.ImageReader) MultiFormatReader(com.google.zxing.MultiFormatReader) Reader(com.google.zxing.Reader) GenericMultipleBarcodeReader(com.google.zxing.multi.GenericMultipleBarcodeReader) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException) ServletException(javax.servlet.ServletException) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) OutputStreamWriter(java.io.OutputStreamWriter) BinaryBitmap(com.google.zxing.BinaryBitmap) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project pancm_project by xuwujing.

the class QrCodeCreateUtil method readQrCode.

/**
 * 读二维码并输出携带的信息
 */
public static void readQrCode(InputStream inputStream) throws IOException {
    // 从输入流中获取字符串信息
    BufferedImage image = ImageIO.read(inputStream);
    // 将图像转换为二进制位图源
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    Result result = null;
    try {
        result = reader.decode(bitmap);
    } catch (ReaderException e) {
        e.printStackTrace();
    }
    System.out.println(result.getText());
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 3 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project elastic-core-maven by OrdinaryDude.

the class DecodeQRCode method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest request) throws NxtException {
    String qrCodeBase64 = Convert.nullToEmpty(request.getParameter("qrCodeBase64"));
    JSONObject response = new JSONObject();
    try {
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(qrCodeBase64))))));
        Map hints = new HashMap();
        hints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.of(BarcodeFormat.QR_CODE));
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result qrCodeData = new MultiFormatReader().decode(binaryBitmap, hints);
        response.put("qrCodeData", qrCodeData.getText());
    } catch (IOException ex) {
        String errorMessage = "Error reading base64 byte stream";
        Logger.logErrorMessage(errorMessage, ex);
        JSONData.putException(response, ex, errorMessage);
    } catch (NullPointerException ex) {
        String errorMessage = "Invalid base64 image";
        Logger.logErrorMessage(errorMessage, ex);
        JSONData.putException(response, ex, errorMessage);
    } catch (NotFoundException ex) {
        response.put("qrCodeData", "");
    }
    return response;
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) HashMap(java.util.HashMap) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) Result(com.google.zxing.Result) JSONObject(org.json.simple.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project portal by ixinportal.

the class parseQRCodeTool method parseQRCode.

public static String[] parseQRCode(InputStream imageInputStream) throws Exception {
    BufferedImage image = ImageIO.read(imageInputStream);
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    Binarizer binarizer = new HybridBinarizer(source);
    BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
    Map<DecodeHintType, Object> hints = new HashMap<>();
    hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
    MultiFormatReader formatReader = new MultiFormatReader();
    Result result = formatReader.decode(binaryBitmap, hints);
    String[] eInvoiceInfo = getEinvoiceInfo(result.getText());
    return eInvoiceInfo;
}
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) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) Binarizer(com.google.zxing.Binarizer) HybridBinarizer(com.google.zxing.common.HybridBinarizer)

Example 5 with BufferedImageLuminanceSource

use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project protools by SeanDragon.

the class MatrixToImageWriterEx method decode.

/**
 * 解码
 *
 * @param filePath
 *
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String decode(String filePath) throws IOException, NotFoundException {
    BufferedImage image;
    image = ImageIO.read(new File(filePath));
    if (image == null) {
        return "Could not decode image";
    }
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Result result;
    Hashtable hints = new Hashtable();
    hints.put(EncodeHintType.CHARACTER_SET, StrConst.DEFAULT_CHARSET_NAME);
    result = new MultiFormatReader().decode(bitmap, hints);
    return result.getText();
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) Hashtable(java.util.Hashtable) BufferedImageLuminanceSource(com.google.zxing.client.j2se.BufferedImageLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) File(java.io.File) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)11 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)11 HybridBinarizer (com.google.zxing.common.HybridBinarizer)11 Result (com.google.zxing.Result)10 MultiFormatReader (com.google.zxing.MultiFormatReader)9 LuminanceSource (com.google.zxing.LuminanceSource)7 BufferedImage (java.awt.image.BufferedImage)6 DecodeHintType (com.google.zxing.DecodeHintType)4 HashMap (java.util.HashMap)4 Binarizer (com.google.zxing.Binarizer)3 ReaderException (com.google.zxing.ReaderException)3 File (java.io.File)3 IOException (java.io.IOException)3 NotFoundException (com.google.zxing.NotFoundException)2 Reader (com.google.zxing.Reader)2 Hashtable (java.util.Hashtable)2 JSONObject (com.alibaba.fastjson.JSONObject)1 ChecksumException (com.google.zxing.ChecksumException)1 FormatException (com.google.zxing.FormatException)1 ImageReader (com.google.zxing.client.j2se.ImageReader)1