use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project document-management-system by openkm.
the class BarcodeTextExtractor method simple.
/**
* Decode only one barcode
*/
@SuppressWarnings("unused")
private String simple(BufferedImage img) throws NotFoundException, ChecksumException, FormatException {
long begin = System.currentTimeMillis();
LuminanceSource source = new BufferedImageLuminanceSource(img);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
com.google.zxing.Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);
SystemProfiling.log(null, System.currentTimeMillis() - begin);
log.trace("simple.Time: {}", System.currentTimeMillis() - begin);
return result.getText();
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project document-management-system by openkm.
the class BarcodeTextExtractor method multiple.
/**
* Decode all barcodes in the image
*/
private String multiple(BufferedImage img) throws NotFoundException, ChecksumException, FormatException {
long begin = System.currentTimeMillis();
LuminanceSource source = new BufferedImageLuminanceSource(img);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
com.google.zxing.Reader reader = new MultiFormatReader();
MultipleBarcodeReader bcReader = new GenericMultipleBarcodeReader(reader);
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
StringBuilder sb = new StringBuilder();
for (Result result : bcReader.decodeMultiple(bitmap, hints)) {
sb.append(result.getText()).append(" ");
}
SystemProfiling.log(null, System.currentTimeMillis() - begin);
log.trace("multiple.Time: {}", System.currentTimeMillis() - begin);
return sb.toString();
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project orion-kit by lijiahangmax.
the class CodeGenerator method decode.
/**
* 解析条码数据
*
* @param image img
* @return data
*/
public String decode(BufferedImage image) {
try {
// 设置实际大小 如果不设置可能会导致解析失败
image = image.getSubimage(0, imgTopMargin, width, height);
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
return new MultiFormatReader().decode(bitmap, this.getDecodeHint()).getText();
} catch (Exception e) {
return null;
}
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project jtelebot by stdmk.
the class Qr method getTextFromQr.
private String getTextFromQr(BufferedImage image) throws NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Result result = new MultiFormatReader().decode(binaryBitmap);
return result.getText();
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project RuleApi by buxia97.
the class QRCodeUtil method parseQRCodeByUrl.
/**
* 根据网络二维码图片解析二维码内容, 区别仅仅在于 ImageIO.read(url); 这一个重载的方法)
*
* @param url 二维码图片网络地址,如 https://res.wx.qq.com/mpres/htmledition/images/mp_qrcode3a7b38.gif
* @return
* @throws Exception
*/
public static String parseQRCodeByUrl(URL url) {
String resultStr = null;
if (url == null) {
return resultStr;
}
try {
/*
* ImageIO 的 BufferedImage read(URL input) 方法用于读取网络图片文件转为内存缓冲图像
* 同理还有:read(File input)、read(InputStream input)、、read(ImageInputStream stream)
* 如果图片网络地址错误,比如不能访问,则 read 抛异常:javax.imageio.IIOException: Can't get input stream from URL!
*/
BufferedImage bufferedImage = ImageIO.read(url);
/*
* com.google.zxing.client.j2se.BufferedImageLuminanceSource:缓冲图像亮度源
* 将 java.awt.image.BufferedImage 转为 zxing 的 缓冲图像亮度源
* 关键就是下面这几句:HybridBinarizer 用于读取二维码图像数据,BinaryBitmap 二进制位图
*/
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable hints = new Hashtable();
/*
* 如果内容包含中文,则解码的字符集格式应该和编码时一致
*/
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
/*
* 如果图片不是二维码图片,则 decode 抛异常:com.google.zxing.NotFoundException
* MultiFormatWriter 的 encode 用于对内容进行编码成 2D 矩阵
* MultiFormatReader 的 decode 用于读取二进制位图数据
*/
Result result = new MultiFormatReader().decode(bitmap, hints);
resultStr = result.getText();
} catch (IOException e) {
e.printStackTrace();
log.error("二维码图片地址错误, 地址是: {}!", url);
} catch (NotFoundException e) {
e.printStackTrace();
log.error("图片非二维码图片, 地址是: {}!", url);
}
return resultStr;
}
Aggregations