提交 874c133b 编写于 作者: 如梦技术's avatar 如梦技术 🐛

mica-qrcode 代码完善。

上级 949a8b32
......@@ -40,6 +40,7 @@ import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Hashtable;
import java.util.Map;
/**
* QRCode 读写,基于 nutz-plugins-qrcode 调整,简化使用
......@@ -403,10 +404,9 @@ public final class QrCode {
* @return QRCode 的图像对象
*/
public BufferedImage toImage() {
String text = new String(content.getBytes(this.encode), this.encode);
BitMatrix matrix;
try {
matrix = new QRCodeWriter().encode(text,
matrix = new QRCodeWriter().encode(content,
BarcodeFormat.QR_CODE,
this.size,
this.size,
......@@ -470,10 +470,63 @@ public final class QrCode {
* @return QRCode 中的内容
*/
public static String read(BufferedImage qrCodeImage) {
return read(qrCodeImage, (Map<DecodeHintType,?>) null);
}
/**
* 从指定的 QRCode 图片文件中解析出其内容。
*
* @param qrCodeFile QRCode 文件
* @return QRCode 中的内容
*/
public static String read(String qrCodeFile, Charset encode) {
return read(ImageUtil.read(qrCodeFile), encode);
}
/**
* 从指定的 QRCode 图片文件中解析出其内容。
*
* @param qrCodeFile QRCode 图片文件
* @return QRCode 中的内容
*/
public static String read(File qrCodeFile, Charset encode) {
return read(ImageUtil.read(qrCodeFile), encode);
}
/**
* 从指定的 QRCode 图片链接中解析出其内容。
*
* @param qrCodeUrl QRCode 图片链接
* @return QRCode 中的内容
*/
public static String read(URL qrCodeUrl, Charset encode) {
return read(ImageUtil.read(qrCodeUrl), encode);
}
/**
* 从指定的 QRCode 图像对象中解析出其内容。
*
* @param qrCodeImage QRCode 图像对象
* @return QRCode 中的内容
*/
public static String read(BufferedImage qrCodeImage, Charset encode) {
Map<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.CHARACTER_SET, encode);
return read(qrCodeImage, hints);
}
/**
* 从指定的 QRCode 图像对象中解析出其内容。
*
* @param qrCodeImage QRCode 图像对象
* @param hints hints
* @return QRCode 中的内容
*/
public static String read(BufferedImage qrCodeImage, Map<DecodeHintType,?> hints) {
LuminanceSource source = new BufferedImageLuminanceSource(qrCodeImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
Result result = new QRCodeReader().decode(bitmap);
Result result = new QRCodeReader().decode(bitmap, hints);
return result.getText();
} catch (NotFoundException | ChecksumException | FormatException e) {
throw Exceptions.unchecked(e);
......@@ -482,6 +535,55 @@ public final class QrCode {
}
}
/**
* 从指定的 QRCode 图片文件中解析出其内容。
*
* @param qrCodeFile QRCode 文件
* @return QRCode 中的内容
*/
public static byte[] readRawBytes(String qrCodeFile) {
return readRawBytes(ImageUtil.read(qrCodeFile));
}
/**
* 从指定的 QRCode 图片文件中解析出其内容。
*
* @param qrCodeFile QRCode 图片文件
* @return QRCode 中的内容
*/
public static byte[] readRawBytes(File qrCodeFile) {
return readRawBytes(ImageUtil.read(qrCodeFile));
}
/**
* 从指定的 QRCode 图片链接中解析出其内容。
*
* @param qrCodeUrl QRCode 图片链接
* @return QRCode 中的内容
*/
public static byte[] readRawBytes(URL qrCodeUrl) {
return readRawBytes(ImageUtil.read(qrCodeUrl));
}
/**
* 从指定的 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();
}
}
private static void addLogo(BufferedImage qrCodeImage, BufferedImage logoImage, QrCode qrCode) {
int baseWidth = qrCodeImage.getWidth();
int baseHeight = qrCodeImage.getHeight();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册