use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project pancm_project by xuwujing.
the class QrCodeCreateUtil method readQrCode.
/**
* 读二维码并输出携带的信息
*
* @param inputStream the input stream
* @throws IOException the io exception
*/
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 pancm_project by xuwujing.
the class QrCodeCreateUtil method decodeQRInfoFromImage.
/**
* 解析图片中的二维码文本信息
*
* @param imagePath 含二维码图片的路径
* @param cachePath 缓存目录路径
* @return 解析出的二维码文本信息
*/
public static String decodeQRInfoFromImage(String imagePath, String cachePath) {
String qrInfo = null;
try {
// 判断图片路径是否为空
if (imagePath == null || "".equals(imagePath.trim())) {
System.err.println("Parameter \'imagePath\' cannot be empty");
qrInfo = null;
return qrInfo;
}
// 判断图片文件是否存在
File imageFile = new File(imagePath);
if (!imageFile.exists()) {
System.err.println("The image file is not exits");
qrInfo = null;
return qrInfo;
}
// 判断是否为图片
try {
Image image = ImageIO.read(imageFile);
if (image == null) {
System.err.println("The image file is not real picture");
qrInfo = null;
return qrInfo;
}
} catch (IOException ex) {
System.err.println("The image file is not real picture");
qrInfo = null;
return qrInfo;
}
// 判断缓存目录是否为空
if (cachePath == null || "".equals(cachePath.trim())) {
System.err.println("Parameter \'cachePath\' cannot be empty");
qrInfo = null;
return qrInfo;
}
// 判断缓存目录是否存在
File cacheFile = new File(cachePath);
if (!cacheFile.exists() || !cacheFile.isDirectory()) {
System.err.println("cachePath is not exits or is not directory");
qrInfo = null;
return qrInfo;
}
ImageIO.setCacheDirectory(new File(cachePath));
BufferedImage image = ImageIO.read(new File(imagePath));
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);
qrInfo = result.getText();
return qrInfo;
} catch (Exception e) {
qrInfo = null;
return qrInfo;
}
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project pancm_project by xuwujing.
the class QrCodeMaxTest method decode.
// qrcode 解码
static void decode(String file) {
try {
Result result = null;
BufferedImage image = null;
image = ImageIO.read(new File(file));
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
result = new MultiFormatReader().decode(bitmap, hints);
String rtn = result.getText();
System.out.println(rtn);
System.out.println(rtn.length());
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project quick-media by liuyueyi.
the class QrCodeDeWrapper method decode.
public static String decode(BufferedImage image) throws FormatException, ChecksumException, NotFoundException {
if (image == null) {
throw new IllegalStateException("can not load qrCode!");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// QRCodeReader qrCodeReader = new QRCodeReader();
// Result result = qrCodeReader.decode(bitmap);
Map<DecodeHintType, Object> hints = new LinkedHashMap<DecodeHintType, Object>();
// 解码设置编码方式为:utf-8,
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
// 优化精度
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
// 复杂模式,开启PURE_BARCODE模式
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
Result result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project tephra by heisedebaise.
the class QrCodeImpl method read.
@Override
public String read(InputStream inputStream) {
try {
String string = reader.decode(new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(inputStream))))).getText();
inputStream.close();
return string;
} catch (Throwable e) {
logger.warn(e, "读取二维码图片内容时发生异常!");
return null;
}
}
Aggregations