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 = ImageIO.read(new File(filePath));
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource 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 "";
}
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 NotFoundException {
if (image == null) {
throw new IllegalStateException("can not load qrCode!");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
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 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 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());
}
Aggregations