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);
}
}
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());
}
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;
}
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;
}
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();
}
Aggregations