条形码?二维码?生成、解析都在这里!
生活随笔
收集整理的這篇文章主要介紹了
条形码?二维码?生成、解析都在这里!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
二維碼生成與解析
一、生成二維碼
二、解析二維碼
三、生成一維碼
四、全部的代碼
五、pom依賴
直接上代碼:
一、生成二維碼
public class demo {private static final String path1="D:\\code.jpg";private static void qr(String text,int width,int weight,String filepath) throws WriterException, IOException {//首先創建一個QRCodeWriter對象QRCodeWriter qrCodeWriter = new QRCodeWriter();//設置二維碼信息:text{二維碼信息},格式,寬度,高度BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE,width,weight);//設置生成的二維碼要保存的地方Path path = FileSystems.getDefault().getPath(filepath);//設置轉換后的格式和地址MatrixToImageWriter.writeToPath(bitMatrix,"JPG",path);}public static void main(String[] args) throws IOException, WriterException {//可以傳網址、文本信息過去qr("www.baidu.com",350,350,path1);}
}
二、解析二維碼
/*** 從一個圖片文件中解碼出二維碼中的內容。* * @param file* @return 解析后的內容。* @throws IOException* @throws ReaderException*/public static final String parseImage(File file) throws IOException, ReaderException {BufferedImage image = ImageIO.read(file);return parseImage(image);}/*** 從圖片中解析出一維碼或者二維碼的內容。如果解析失敗,則拋出NotFoundException。* @param image* @return* @throws NotFoundException*/public static final String parseImage(BufferedImage image) throws NotFoundException {LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap bitmap = new BinaryBitmap(binarizer);Result result = READER.decode(bitmap);// 這里丟掉了Result中其他一些數據return result.getText();}
三、生成一維碼
/*** 將字符串編碼成一維碼(條形碼)。* @param content* @return* @throws WriterException* @throws IOException*/public static BufferedImage createBarCode(String content) throws WriterException, IOException {MultiFormatWriter writer = new MultiFormatWriter();// 一維碼的寬>高。這里我設置為 寬:高=2:1BitMatrix matrix = writer.encode(content, BarcodeFormat.EAN_13, BARCODE_WIDTH * 3, BARCODE_WIDTH);return toBufferedImage(matrix);}/*** 將一個BitMatrix對象轉換成BufferedImage對象* * @param matrix* @return*/private static BufferedImage toBufferedImage(BitMatrix matrix) {int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);}}return image;}
四、接著出一下全部的
package com.demo.ajax.demo;
/*** 二維碼生成類(可生成二維碼和條形碼)*/
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import javax.servlet.http.HttpServletResponse;public class QRCodeUtil {// 這幾項可以由其他調用的類設置,因此是public static的public static int BARCODE_WIDTH = 80;public static int QRCODE_WIDTH = 200;public static String FORMAT = "jpg";// 生成的圖片格式public static int BLACK = 0x000000;// 編碼的顏色public static int WHITE = 0xFFFFFF;// 空白的顏色// 二維碼中間的圖像配置。注意,由于二維碼的容錯率有限,因此中間遮擋的面積不要太大,否則可能解析不出來。private static int ICON_WIDTH = (int)(QRCODE_WIDTH / 6);private static int HALF_ICON_WIDTH = ICON_WIDTH / 2;private static int FRAME_WIDTH = 2;// Icon四周的邊框寬度// 二維碼讀碼器和寫碼器private static final MultiFormatWriter WRITER = new MultiFormatWriter();private static final MultiFormatReader READER = new MultiFormatReader();// 測試public static void main(String[] args) throws Exception {/*** 二維碼測試。*/String iconPath = "D:\\code.jpg";String content = "http://www.baidu.com";File qrCode = new File("/home/lxf/qrcode/qrcode." + FORMAT);File qrCodeWithIcon = new File("/home/lxf/qrcode/qrcode_img." + FORMAT);// 生成二維碼writeToFile(createQRCode(content), qrCode);// 生成帶圖標的二維碼writeToFile(createQRCodeWithIcon(content, iconPath), qrCodeWithIcon);// 解析二維碼System.out.println(parseImage(qrCode));// 解析帶圖標的二維碼System.out.println(parseImage(qrCodeWithIcon));// 編碼成字節數組byte[] data = createQRCodeToBytes(content);String result = parseQRFromBytes(data);System.out.println(result);/*** 一維碼測試。*/String barCodeContent="6936983800013";File barCode = new File("C:\\BarCode." + FORMAT);// 生成一維碼writeToFile(createBarCode(barCodeContent), barCode);// 解析一維碼System.out.println(parseImage(barCode));}/*** 將String編碼成二維碼的圖片后,使用字節數組表示,便于傳輸。* * @param content* @return* @throws WriterException* @throws IOException*/public static byte[] createQRCodeToBytes(String content) throws WriterException, IOException {BufferedImage image = createQRCode(content);ByteArrayOutputStream os = new ByteArrayOutputStream();ImageIO.write(image, FORMAT, os);return os.toByteArray();}/*** 把一個String編碼成二維碼的BufferedImage.* * @param content* @return* @throws WriterException*/public static final BufferedImage createQRCode(String content) throws WriterException {// 長和寬一樣,所以只需要定義一個SIZE即可BitMatrix matrix = WRITER.encode(content, BarcodeFormat.QR_CODE, QRCODE_WIDTH, QRCODE_WIDTH);return toBufferedImage(matrix);}/*** 編碼字符串為二維碼,并在該二維碼中央插入指定的圖標。* @param content* @param iconPath* @return* @throws WriterException*/public static final BufferedImage createQRCodeWithIcon(String content, String iconPath) throws WriterException {BitMatrix matrix = WRITER.encode(content, BarcodeFormat.QR_CODE, QRCODE_WIDTH, QRCODE_WIDTH);// 讀取Icon圖像BufferedImage scaleImage = null;try {scaleImage = scaleImage(iconPath, ICON_WIDTH, ICON_WIDTH, true);} catch (IOException e) {e.printStackTrace();}int[][] iconPixels = new int[ICON_WIDTH][ICON_WIDTH];for (int i = 0; i < scaleImage.getWidth(); i++) {for (int j = 0; j < scaleImage.getHeight(); j++) {iconPixels[i][j] = scaleImage.getRGB(i, j);}}// 二維碼的寬和高int halfW = matrix.getWidth() / 2;int halfH = matrix.getHeight() / 2;// 計算圖標的邊界:int minX = halfW - HALF_ICON_WIDTH;//左int maxX = halfW + HALF_ICON_WIDTH;//右int minY = halfH - HALF_ICON_WIDTH;//上int maxY = halfH + HALF_ICON_WIDTH;//下int[] pixels = new int[QRCODE_WIDTH * QRCODE_WIDTH];// 修改二維碼的字節信息,替換掉一部分為圖標的內容。for (int y = 0; y < matrix.getHeight(); y++) {for (int x = 0; x < matrix.getWidth(); x++) {// 如果點在圖標的位置,用圖標的內容替換掉二維碼的內容if (x > minX && x < maxX && y > minY && y < maxY) {int indexX = x - halfW + HALF_ICON_WIDTH;int indexY = y - halfH + HALF_ICON_WIDTH;pixels[y * QRCODE_WIDTH + x] = iconPixels[indexX][indexY];}// 在圖片四周形成邊框else if ((x > minX - FRAME_WIDTH && x < minX + FRAME_WIDTH && y > minY - FRAME_WIDTH && y < maxY + FRAME_WIDTH)|| (x > maxX - FRAME_WIDTH && x < maxX + FRAME_WIDTH && y > minY - FRAME_WIDTH && y < maxY + FRAME_WIDTH)|| (x > minX - FRAME_WIDTH && x < maxX + FRAME_WIDTH && y > minY - FRAME_WIDTH && y < minY + FRAME_WIDTH)|| (x > minX - FRAME_WIDTH && x < maxX + FRAME_WIDTH && y > maxY - FRAME_WIDTH && y < maxY + FRAME_WIDTH)) {pixels[y * QRCODE_WIDTH + x] = WHITE;}else {// 這里是其他不屬于圖標的內容。即為二維碼沒有被圖標遮蓋的內容,用矩陣的值來顯示顏色。pixels[y * QRCODE_WIDTH + x] = matrix.get(x, y) ? BLACK : WHITE;}}}// 用修改后的字節數組創建新的BufferedImage.BufferedImage image = new BufferedImage(QRCODE_WIDTH, QRCODE_WIDTH, BufferedImage.TYPE_INT_RGB);image.getRaster().setDataElements(0, 0, QRCODE_WIDTH, QRCODE_WIDTH, pixels);return image;}/*** 從一個二維碼圖片的字節信息解碼出二維碼中的內容。* * @param data* @return* @throws ReaderException* @throws IOException*/public static String parseQRFromBytes(byte[] data) throws ReaderException, IOException {ByteArrayInputStream is = new ByteArrayInputStream(data);BufferedImage image = ImageIO.read(is);return parseImage(image);}/*** 從一個圖片文件中解碼出二維碼中的內容。* * @param file* @return 解析后的內容。* @throws IOException* @throws ReaderException*/public static final String parseImage(File file) throws IOException, ReaderException {BufferedImage image = ImageIO.read(file);return parseImage(image);}/*** 將字符串編碼成一維碼(條形碼)。* @param content* @return* @throws WriterException* @throws IOException*/public static BufferedImage createBarCode(String content) throws WriterException, IOException {MultiFormatWriter writer = new MultiFormatWriter();// 一維碼的寬>高。這里我設置為 寬:高=2:1BitMatrix matrix = writer.encode(content, BarcodeFormat.EAN_13, BARCODE_WIDTH * 3, BARCODE_WIDTH);return toBufferedImage(matrix);}/*** 從圖片中解析出一維碼或者二維碼的內容。如果解析失敗,則拋出NotFoundException。* @param image* @return* @throws NotFoundException*/public static final String parseImage(BufferedImage image) throws NotFoundException {LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap bitmap = new BinaryBitmap(binarizer);Result result = READER.decode(bitmap);// 這里丟掉了Result中其他一些數據return result.getText();}/*** 將BufferedImage對象輸出到指定的文件中。* * @param image* @param destFile* @throws IOException*/public static final void writeToFile(BufferedImage image, File destFile) throws IOException {ImageIO.write(image, FORMAT, destFile);}/*** 將BufferedImage對象直接response* @param image* @param response* @throws IOException*/public static final void showQrcode(BufferedImage image, HttpServletResponse response) throws IOException{ImageIO.write(image, FORMAT, response.getOutputStream());}/*** 將一個BitMatrix對象轉換成BufferedImage對象* * @param matrix* @return*/private static BufferedImage toBufferedImage(BitMatrix matrix) {int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);}}return image;}/*** 把傳入的原始圖像按高度和寬度進行縮放,生成符合要求的圖標。* * @param srcImageFile 源文件地址* @param height 目標高度* @param width 目標寬度* @param hasFiller 比例不對時是否需要補白:true為補白; false為不補白;* @throws IOException*/private static BufferedImage scaleImage(String srcImageFile, int height, int width, boolean hasFiller) throws IOException {double ratio = 0.0; // 縮放比例File file = new File(srcImageFile);BufferedImage srcImage = ImageIO.read(file);Image destImage = srcImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);// 計算比例if ((srcImage.getHeight() > height) || (srcImage.getWidth() > width)) {if (srcImage.getHeight() > srcImage.getWidth()) {ratio = (new Integer(height)).doubleValue() / srcImage.getHeight();} else {ratio = (new Integer(width)).doubleValue() / srcImage.getWidth();}AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);destImage = op.filter(srcImage, null);}if (hasFiller) {// 補白BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D graphic = image.createGraphics();graphic.setColor(Color.white);graphic.fillRect(0, 0, width, height);if (width == destImage.getWidth(null)) {graphic.drawImage(destImage, 0, (height - destImage.getHeight(null)) / 2, destImage.getWidth(null), destImage.getHeight(null), Color.white, null);} else {graphic.drawImage(destImage, (width - destImage.getWidth(null)) / 2, 0, destImage.getWidth(null), destImage.getHeight(null), Color.white, null);}graphic.dispose();destImage = image;}return (BufferedImage) destImage;}
}
五、pom依賴
<!-- https://mvnrepository.com/artifact/com.google.zxing/core --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.0</version></dependency><!-- https://mvnrepository.com/artifact/com.google.zxing/javase --><!-- https://mvnrepository.com/artifact/com.google.zxing/javase --><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.4.0</version></dependency>
總結
以上是生活随笔為你收集整理的条形码?二维码?生成、解析都在这里!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pom文件中引入常用的maven仓库
- 下一篇: Linux安装mysql,一步到位!