use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project ngtesting-platform by aaronchen2k.
the class QrcodeServiceImpl method decode.
@Override
public JSONObject decode(String filePath) {
BufferedImage image;
try {
image = ImageIO.read(new File(filePath));
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);
System.out.println(result.getText());
JSONObject content = JSONObject.parseObject(result.getText());
System.out.println("encode: " + result.getBarcodeFormat());
return content;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
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 springboot-demo by small-rose.
the class QRCodeUtil method decode.
/**
* 解码,将二维码里的信息解码出来
* @param path
* @return
* @throws Exception
*/
public static String decode(String path) throws Exception {
File file = new File(path);
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project DevKits by qmjy.
the class CodecFrame method decodeBufferedImage.
@SuppressWarnings({ "rawtypes", "unchecked" })
private Optional<Result> decodeBufferedImage(BufferedImage bufferedImage) {
try {
/**
* 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 用于读取二进制位图数据
*/
return Optional.of(new MultiFormatReader().decode(bitmap, hints));
} catch (NotFoundException e) {
LOGGER.error(" Any errors which occurred...");
}
return Optional.empty();
}
use of com.google.zxing.client.j2se.BufferedImageLuminanceSource in project mica by lets-mica.
the class QrCode method readRawBytes.
/**
* 从指定的 QRCode 图像对象中解析出其内容。
*
* @param qrCodeImage QRCode 图像对象
* @return QRCode 中的内容
*/
public static byte[] readRawBytes(BufferedImage qrCodeImage) {
LuminanceSource source = new BufferedImageLuminanceSource(qrCodeImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
Result result = new QRCodeReader().decode(bitmap);
return result.getRawBytes();
} catch (NotFoundException | ChecksumException | FormatException e) {
throw Exceptions.unchecked(e);
} finally {
qrCodeImage.getGraphics().dispose();
}
}
Aggregations