Java生成讀取條形碼和二維碼的簡單示例
條形碼
將寬度不等的多個黑條和白條,按照一定的編碼規(guī)則排序,用以表達一組信息的圖像標識符
通常代表一串數(shù)字 / 字母,每一位有特殊含義
一般數(shù)據(jù)容量30個數(shù)字 / 字母
二維碼
用某種特定幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號信息
比一維條形碼能存儲更多信息,表示更多數(shù)據(jù)類型
能夠存儲數(shù)字 / 字母 / 漢字 / 圖片等信息
可存儲幾百到幾十KB字符
Zxing
Zxing主要是Google出品的,用于識別一維碼和二維碼的第三方庫
主要類:
- BitMatrix位圖矩陣
- MultiFormatWriter位圖編寫器
- MatrixToImageWriter寫入圖片
Maven導入Zxing
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
生成一維碼java
public static void main(String[] args) {
generateCode(new File("1dcode.png"), "1390351289", 500, 250);
}
/**
* @param file 生成的文件名稱
* @param code 一維碼存儲的數(shù)據(jù)信息
* @param width 生成圖片的寬度
* @param height 生成圖片的高度
* @return void
* */
public static void generateCode(File file, String code, int width, int height){
// 定義位圖矩陣BitMatrix
BitMatrix matrix = null;
try {
// 使用code_128格式進行編碼生成100*25的條形碼
MultiFormatWriter writer = new MultiFormatWriter();
matrix = writer.encode(code, BarcodeFormat.CODE_128, width, height, null);
} catch (WriterException e) {
e.printStackTrace();
}
// 將位圖矩陣BitMatrix保存為圖片
try {
FileOutputStream outputStream = new FileOutputStream(file);
ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "png", outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
注意:一維碼只能存儲數(shù)字和字母,其他數(shù)據(jù)會報Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project MavenDemo: Command execution failed.錯誤java
讀取一維碼
public static void main(String[] args) {
readCode(new File("1dcode.png"));
}
/**
* @param readImage 讀取一維碼圖片名
* @return void
* */
public static void readCode(File readImage) {
try {
BufferedImage image = ImageIO.read(readImage);
if (image == null) {
return;
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "gbk");
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = new MultiFormatReader().decode(bitmap, hints);
System.out.println(result.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
注意:當使用String類進行轉碼時,要使用Java.lang包的,Maven導包的時候會導入第三方Apache的String類
生成二維碼
/** 定義二維碼的寬度 */
private final static int WIDTH = 300;
/** 定義二維碼的高度 */
private final static int HEIGHT = 300;
/** 定義二維碼的格式 */
private final static String FORMAT = "png";
/**
* @param file
* @param content
* @return void
* */
public static void generateQRCode(File file, String content) {
// 定義二維碼參數(shù)
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 設置編碼
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 設置容錯等級
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
// 設置邊距,默認為5
hints.put(EncodeHintType.MARGIN, 2);
try {
BitMatrix bitMatrix = new MultiFormatWriter()
.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
Path path = file.toPath();
// 保存到項目跟目錄中
MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
generateQRCode(new File("smt.png"), "淑玫唐家居網(wǎng)");
}
讀取二維碼
/**
* @param file 讀取二維碼的文件名
* @return void
* */
public static void readQRCode(File file) {
MultiFormatReader reader = new MultiFormatReader();
try {
BufferedImage image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = reader.decode(binaryBitmap, hints);
System.out.println("解析結果: " + new String(result.toString().getBytes("GBK"), "GBK"));
System.out.println("二維碼格式: " + result.getBarcodeFormat());
System.out.println("二維碼文本內(nèi)容: " + new String(result.getText().getBytes("GBK"), "GBK"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
readQRCode(new File("smt.png"));
}
注意: Maven打印的控制臺中會出現(xiàn)中文亂碼,在IDEA Setting->maven->runner VMoptions:-Dfile.encoding=GB2312;即可解決
總結
到此這篇關于Java生成讀取條形碼和二維碼的文章就介紹到這了,更多相關Java生成讀取條形碼二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Mybatis多數(shù)據(jù)源切換實現(xiàn)代碼
這篇文章主要介紹了Mybatis多數(shù)據(jù)源切換實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10
springboot整合curator實現(xiàn)分布式鎖過程
這篇文章主要介紹了springboot整合curator實現(xiàn)分布式鎖過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
使用ServletInputStream()輸入流讀取圖片方式
這篇文章主要介紹了使用ServletInputStream()輸入流讀取圖片方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

