条形码(barcode)是将宽度不等来源gao($daima.com搞@代@#码网的多个黑条和空白,按照一定的规则排列,用来表示一组信息的图形标识符,而二维码大家应该都很熟悉了,这篇文章主要给大家介绍了关于Java生成读取条形码和二维码的相关资料,需要的朋友可以参考下
条形码
将宽度不等的多个黑条和白条,按照一定的编码规则排序,用以表达一组信息的图像标识符
通常代表一串数字 / 字母,每一位有特殊含义
一般数据容量30个数字 / 字母
二维码
用某种特定几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息
比一维条形码能存储更多信息,表示更多数据类型
能够存储数字 / 字母 / 汉字 / 图片等信息
可存储几百到几十KB字符
Zxing
Zxing主要是Google出品的,用于识别一维码和二维码的第三方库
主要类:
- BitMatrix位图矩阵
- MultiFormatWriter位图编写器
- MatrixToImageWriter写入图片
Maven导入Zxing
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase --> com.google.zxingjavase3.2.1 com.google.zxingcore3.0.0
生成一维码java
public static void main(String[] args) { generateCode(new File("1dcode.png-600"), "1390351289", 500, 250); } /** * @param file 生成的文件名称 * @param code 一维码存储的数据信息 * @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(); } }
注意:一维码只能存储数字和字母,其他数据会报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-600")); } /** * @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 hints = new HashMap(); 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) { // 定义二维码参数 Map hints = new HashMap(); // 设置编码 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-600"), "淑玫唐家居网"); }
读取二维码
/** * @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 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("二维码文本内容: " + new String(result.getText().getBytes("GBK"), "GBK")); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { readQRCode(new File("smt.png-600")); }
注意: Maven打印的控制台中会出现中文乱码,在IDEA Setting->maven->runner VMoptions:-Dfile.encoding=GB2312;即可解决
总结
到此这篇关于Java生成读取条形码和二维码的文章就介绍到这了,更多相关Java生成读取条形码二维码内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网!
以上就是Java生成读取条形码和二维码的简单示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!