java 二維碼生成+解析 工具zxing

有l(wèi)ogo? 圖片

package com.palmble.common.zxing;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.geom.RoundRectangle2D;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

public class LogoUtil {

? ? public static BufferedImage logoMatrix(BufferedImage bufferedImage,String logopath) throws IOException {

? ? ? ? //在二維碼上畫logo:產(chǎn)生一個 二維碼畫板

? ? ? ? Graphics2D graphics2D=bufferedImage.createGraphics();

? ? ? ? BufferedImage logoImg= ImageIO.read(new File(logopath));

? ? ? ? int width=bufferedImage.getWidth();

? ? ? ? int height=bufferedImage.getHeight();

? ? ? ? //logo

? ? ? ? graphics2D.drawImage(logoImg,width*2/5,height*2/5,width*1/5,height*1/5,null);

? ? ? ? //畫筆

? ? ? ? BasicStroke stroke=new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);

? ? ? ? //關聯(lián)畫筆

? ? ? ? graphics2D.setStroke(stroke);

? ? ? ? //創(chuàng)建一個正方形

? ? ? ? RoundRectangle2D.Float round=new RoundRectangle2D.Float(width*2/5,height*2/5,width*1/5,height*1/5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);

? ? ? ? graphics2D.setColor(Color.WHITE);//白色

? ? ? ? graphics2D.draw(round);

? ? ? ? //灰色

? ? ? ? BasicStroke stroke2=new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);

? ? ? ? graphics2D.setStroke(stroke2);

? ? ? ? RoundRectangle2D.Float round2=new RoundRectangle2D.Float(width*2/5+2,height*2/5+2,width*1/5-4,height*1/5-4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);

? ? ? ? graphics2D.setColor(Color.GRAY);//灰色

? ? ? ? graphics2D.draw(round2);

? ? ? ? graphics2D.dispose();

? ? ? ? bufferedImage.flush();

? ? ? ? return? bufferedImage;

? ? }

}



二維碼生成和讀的工具類 使用google 開發(fā)的 zxing

package com.palmble.common.zxing;

import com.google.zxing.*;

import com.google.zxing.client.j2se.MatrixToImageConfig;

import com.google.zxing.client.j2se.MatrixToImageWriter;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.common.HybridBinarizer;

import com.google.zxing.qrcode.QRCodeReader;

import com.google.zxing.qrcode.QRCodeWriter;

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import com.google.zxing.client.j2se.BufferedImageLuminanceSource;

import java.io.*;

import java.util.EnumMap;

import java.util.HashMap;

import java.util.Hashtable;

import java.util.Map;

/**

*

* 二維碼生成和讀的工具類 使用google 開發(fā)的 zxing

* 參數(shù)介紹:

*? ? ? ZXing采用Hashtable方式來保存設置參數(shù):糾錯能力為 L 級別,設置編碼類型為UTF-8

*? ? ? 位矩陣二維碼數(shù)據(jù) BitMatrix

*/

public class QrCodeUtil {

? ? private String content;? ? ? ? ? ? ? ? //二維碼內(nèi)容

? ? private String qrCodeUrl;? ? ? ? ? //二維碼網(wǎng)絡路徑

? ? private String filePath;? ? ? ? ? //二維碼生成物理路徑 也可以是文件的絕對路徑

? ? private String fileName;? ? ? ? ? //二維碼生成圖片名稱(包含后綴名)

? ? private String imageFormat;? ? ? //二維碼圖片后綴名(jpg、png)

? ? private String logoPath;? ? ? ? ? //logo圖片

? ? private Integer width = 300;? ? ? ? ? //二維碼寬度

? ? private Integer height = 300;? ? ? ? ? //二維碼高度

? ? private Integer onColor = 0xFF000000;? //前景色 黑色

? ? private Integer bgColor = 0xFFFFFFFF; //背景色 白色

? ? private Integer margin = 2;? ? ? ? ? ? //白邊大小,取值范圍0~4

? ? private ErrorCorrectionLevel level = ErrorCorrectionLevel.M;? //二維碼容錯率指 糾錯級別(L 7%、M 15%、Q 25%、H 30%)

? ? /**

? ? * 生成二維碼 屬性:content、fileName、filePath不得為空

? ? * @throws Exception

? ? */

? ? public void createQRCode() throws Exception {

? ? ? ? String imgPath = this.getFilePath();

? ? ? ? String imgName = this.getFileName();

? ? ? ? String content = this.getContent();

? ? ? ? String imageFormat = this.getImageFormat();

? ? ? ? if (imageFormat == null || imgPath == null || content == null) throw new Exception("參數(shù)錯誤");

? ? ? ? boolean flag;

? ? ? ? String[]? arr=filePath.split("\\.");

? ? ? ? if(arr.length==1) flag=true;

? ? ? ? else if(arr.length==2) flag=false;

? ? ? ? else throw new Exception("判斷路徑類型錯誤");

? ? ? ? if(flag) {//imgPath 是物理路徑

? ? ? ? ? ? if (this.getLogoPath() != null && !"".equals(this.getLogoPath().trim()))

? ? ? ? ? ? ? ? generateQRCodeWithLogo(content, this.getLogoPath(), imgPath, imgName, imageFormat);

? ? ? ? ? ? else generateQRCode(content, imgPath, imgName, imageFormat);

? ? ? ? }else{//imgPath 是絕對路徑

? ? ? ? ? ? if (this.getLogoPath() != null && !"".equals(this.getLogoPath().trim())) generateQRCodeWithLogo(content, this.getLogoPath(), imgPath, imageFormat);

? ? ? ? ? ? else generateQRCode(content, imgPath, imageFormat);

? ? ? ? }

? ? }

? ? /**

? ? * 生成二維碼

? ? * @param content? ? ? //二維碼內(nèi)容

? ? * @param imgPath? ? ? //二維碼保存物理路徑

? ? * @param imgName? ? ? //二維碼文件名稱

? ? * @param suffix? ? ? //圖片后綴名

? ? */

? ? public void generateQRCode(String content, String imgPath, String imgName, String suffix) throws Exception{

? ? ? ? File filePath = new File(imgPath);

? ? ? ? if(!filePath.exists()){

? ? ? ? ? ? throw new Exception("文件路徑不存在");

? ? ? ? }

? ? ? ? File imageFile = new File(imgPath,imgName);

? ? ? ? Hashtable<EncodeHintType, Object> hints = new Hashtable<>();

? ? ? ? hints.put(EncodeHintType.ERROR_CORRECTION, level); // 指定糾錯等級

? ? ? ? hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定編碼格式

? ? ? ? hints.put(EncodeHintType.MARGIN, this.getMargin());//設置白邊

? ? ? ? MatrixToImageConfig config = new MatrixToImageConfig( this.getOnColor() , this.getBgColor());

? ? ? ? BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, this.getWidth(), this.getHeight(), hints);

//? ? ? bitMatrix = deleteWhite(bitMatrix);

? ? ? ? MatrixToImageWriter.writeToPath(bitMatrix, suffix, imageFile.toPath(), config);

? ? }

? ? /**

? ? * 生成二維碼

? ? * @param content? ? ? //二維碼內(nèi)容

? ? * @param path? ? ? ? ? //二維碼保存文件 絕對路徑 如:d:\\qrcode.jpg)

? ? * @param suffix? ? ? //圖片后綴名

? ? */

? ? public void generateQRCode(String content, String path, String suffix) throws Exception{

? ? ? ? File imageFile = new File(path);

? ? ? ? if (!imageFile.getParentFile().exists()) {

? ? ? ? ? ? imageFile.getParentFile().mkdirs();

? ? ? ? }

? ? ? ? Hashtable<EncodeHintType, Object> hints = new Hashtable<>();

? ? ? ? hints.put(EncodeHintType.ERROR_CORRECTION, level); // 指定糾錯等級

? ? ? ? hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定編碼格式

? ? ? ? hints.put(EncodeHintType.MARGIN, this.getMargin());//設置白邊

? ? ? ? MatrixToImageConfig config = new MatrixToImageConfig( this.getOnColor() , this.getBgColor());

? ? ? ? BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, this.getWidth(), this.getHeight(), hints);

? ? ? ? MatrixToImageWriter.writeToPath(bitMatrix, suffix, imageFile.toPath(), config);

? ? }

? ? /**

? ? * 生成帶logo的二維碼圖片

? ? * @param content? ? ? //二維碼內(nèi)容

? ? * @param logoPath? ? //logo絕對物理路徑

? ? * @param imgPath? ? ? //二維碼保存絕對物理路徑

? ? * @param imgName? ? ? //二維碼文件名稱

? ? * @param suffix? ? ? //圖片后綴名

? ? * @throws Exception

? ? */

? ? public void generateQRCodeWithLogo(String content, String logoPath, String imgPath, String imgName, String suffix) throws Exception{

? ? ? ? File filePath = new File(imgPath);

? ? ? ? if(!filePath.exists()){

? ? ? ? ? ? filePath.mkdirs();

? ? ? ? }

? ? ? ? if(imgPath.endsWith("/")){

? ? ? ? ? ? imgPath += imgName;

? ? ? ? }else{

? ? ? ? ? ? imgPath += "/"+imgName;

? ? ? ? }

? ? ? ? Hashtable<EncodeHintType, Object> hints = new Hashtable<>();

? ? ? ? hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

? ? ? ? hints.put(EncodeHintType.ERROR_CORRECTION, this.getLevel());

? ? ? ? hints.put(EncodeHintType.MARGIN, this.getMargin());? //設置白邊

? ? ? ? BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.getWidth(), this.getHeight(), hints);

? ? ? ? File qrcodeFile = new File(imgPath);

? ? ? ? writeToFile(toBufferedImage(bitMatrix), suffix, qrcodeFile, logoPath);

? ? }

? ? /**

? ? * 生成帶logo的二維碼圖片

? ? * @param content? ? ? //二維碼內(nèi)容

? ? * @param logoPath? ? //logo絕對物理路徑

? ? * @param path? ? ? ? //二維碼保存文件 絕對路徑 如:d:\\qrcode.jpg)

? ? * @param suffix? ? ? //圖片后綴名

? ? * @throws Exception

? ? */

? ? public void generateQRCodeWithLogo(String content, String logoPath, String path, String suffix) throws Exception{

? ? ? ? Hashtable<EncodeHintType, Object> hints = new Hashtable<>();

? ? ? ? hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

? ? ? ? hints.put(EncodeHintType.ERROR_CORRECTION, this.getLevel());

? ? ? ? hints.put(EncodeHintType.MARGIN, this.getMargin());? //設置白邊

? ? ? ? BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.getWidth(), this.getHeight(), hints);

? ? ? ? File qrcodeFile = new File(path);

? ? ? ? writeToFile(toBufferedImage(bitMatrix), suffix, qrcodeFile, logoPath);

? ? }

? ? /**

? ? * @param image 二維碼矩陣相關

? ? * @param format 二維碼圖片格式

? ? * @param file 二維碼圖片文件

? ? * @param logoPath logo路徑

? ? * @throws IOException

? ? */

? ? public? void writeToFile( BufferedImage image,String format,File file,String logoPath) throws Exception {

? ? ? ? //讀取二維碼圖片

? ? ? ? Graphics2D gs = image.createGraphics();

? ? ? ? int ratioWidth = image.getWidth()*2/10;

? ? ? ? int ratioHeight = image.getHeight()*2/10;

? ? ? ? //載入logo

? ? ? ? Image img = ImageIO.read(new File(logoPath));

? ? ? ? //設置二維碼覆蓋(logo大?。?,太大會覆蓋二維碼,此處20%

? ? ? ? int logoWidth = img.getWidth(null)>ratioWidth?ratioWidth:img.getWidth(null);

? ? ? ? int logoHeight = img.getHeight(null)>ratioHeight?ratioHeight:img.getHeight(null);

? ? ? ? //設置logo圖片放置位置

? ? ? ? int x = (image.getWidth() - logoWidth) / 2;

? ? ? ? int y = (image.getHeight() - logoHeight) / 2;

? ? ? ? gs.drawImage(img, x, y, logoWidth, logoHeight, null);

? ? ? ? //logo邊框大小

? ? ? ? gs.setStroke(new BasicStroke(1));

? ? ? ? // gs.setColor(Color.black);

//? ? ? ? gs.drawRect(x, y, logoWidth, logoHeight);

? ? ? ? gs.setBackground(Color.WHITE);

? ? ? ? gs.dispose();

? ? ? ? img.flush();

? ? ? ? if(!ImageIO.write(image, format, file)){

? ? ? ? ? ? throw new Exception("不能把圖片"+file+"轉成" + format + " 格式 ");

? ? ? ? }

? ? }

? ? //把數(shù)據(jù)放入圖片緩沖區(qū)

? ? public 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) ? this.getOnColor() : this.getBgColor());

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return image;

? ? }

? ? /**

? ? * 讀二維碼并輸出攜帶的信息

? ? */

? ? public String readQrCode() throws Exception{

? ? ? ? File filePath = new File(this.getFilePath());

? ? ? ? if(!filePath.exists()){

? ? ? ? ? ? throw new Exception("文件不存在");

? ? ? ? }

? ? ? ? InputStream inputStream=new FileInputStream(filePath);

? ? ? ? //從輸入流中獲取字符串信息

? ? ? ? BufferedImage image = ImageIO.read(inputStream);

? ? ? ? //將圖像轉換為二進制位圖源

? ? ? ? LuminanceSource source = new BufferedImageLuminanceSource(image);

? ? ? ? BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

? ? ? ? QRCodeReader reader = new QRCodeReader();

? ? ? ? Result result = reader.decode(bitmap);

? ? ? ? return result.getText();

? ? }

? ? /**

? ? * 生成包含字符串信息的二維碼圖片

? ? * @param saveImgPath 文件輸出流路徑

? ? * @param content 二維碼攜帶信息

? ? * @param qrCodeWidth 二維碼圖片寬度

? ? * @param qrCodeHeight 二維碼圖片高度

? ? * @param imageFormat 二維碼的格式

? ? * @throws WriterException

? ? * @throws IOException

? ? */

? ? public static boolean createQrCode(String saveImgPath, String content, int qrCodeWidth, int qrCodeHeight, String imageFormat) throws Exception{

? ? ? ? File filePath = new File(saveImgPath);

? ? ? ? OutputStream outputStream=new FileOutputStream(filePath);

? ? ? ? Hashtable<EncodeHintType, Object> hashtable = new Hashtable<>();//

? ? ? ? hashtable.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 指定糾錯等級,糾錯級別(L 7%、M 15%、Q 25%、H 30%)

? ? ? ? hashtable.put(EncodeHintType.CHARACTER_SET, "utf-8");// 使用字符集編碼

? ? ? ? hashtable.put(EncodeHintType.MARGIN, 1);//設置二維碼邊的空度,非負數(shù)

? ? ? ? QRCodeWriter qrCodeWriter = new QRCodeWriter();

? ? ? ? //創(chuàng)建比特矩陣(位矩陣)的QR碼編碼的字符串

? ? ? ? BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, hashtable);

? ? ? ? // 使BufferedImage勾畫QRCode? (matrixWidth 是行二維碼像素點)

? ? ? ? int matrixWidth = bitMatrix.getWidth();

? ? ? ? BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);

? ? ? ? image.createGraphics();

? ? ? ? Graphics2D graphics = (Graphics2D) image.getGraphics();

? ? ? ? graphics.setColor(Color.WHITE);

? ? ? ? graphics.fillRect(0, 0, matrixWidth, matrixWidth);

? ? ? ? graphics.setColor(Color.BLACK);

? ? ? ? // 使用比特矩陣畫并保存圖像

? ? ? ? for (int i = 0; i < matrixWidth; i++){

? ? ? ? ? ? for (int j = 0; j < matrixWidth; j++){

? ? ? ? ? ? ? ? if (bitMatrix.get(i, j)){

? ? ? ? ? ? ? ? ? ? graphics.fillRect(i-100, j-100, 1, 1);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return ImageIO.write(image, imageFormat, outputStream);

? ? }

? ? /**

? ? * 讀二維碼并輸出攜帶的信息

? ? */

? ? public static String readQrCode(String imgPath) throws Exception{

? ? ? ? File filePath = new File(imgPath);

? ? ? ? if(!filePath.exists()){

? ? ? ? ? ? throw new Exception("文件不存在");

? ? ? ? }

? ? ? ? InputStream inputStream=new FileInputStream(filePath);

? ? ? ? //從輸入流中獲取字符串信息

? ? ? ? BufferedImage image = ImageIO.read(inputStream);

? ? ? ? //將圖像轉換為二進制位圖源

? ? ? ? LuminanceSource source = new BufferedImageLuminanceSource(image);

? ? ? ? BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

? ? ? ? QRCodeReader reader = new QRCodeReader();

? ? ? ? Result result = reader.decode(bitmap);

? ? ? ? return result.getText();

? ? }

? ? /**

? ? * 測試代碼

? ? * @throws Exception

? ? */

? ? public static void main(String[] args) throws Exception {

? ? ? ? //靜態(tài)方法

? ? ? ? //createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")),"德瑪西亞",200,200,"JPEG");

? ? ? ? //readQrCode("d:\\142.png");

? ? ? ? //使用對象 生成二維碼

? ? ? ? QrCodeUtil qrCodeUtil=new QrCodeUtil();

? ? ? ? qrCodeUtil.setContent("在海南的朋友們想買野生土蜂蜜可以找我喲?。。。?);

? ? ? ? qrCodeUtil.setFilePath("d:/qrcode.jpg");

? ? ? ? qrCodeUtil.setImageFormat("jpeg");

? ? ? ? qrCodeUtil.setLogoPath("d:/1.png");

? ? ? ? qrCodeUtil.setWidth(300);

? ? ? ? qrCodeUtil.setHeight(300);

? ? ? ? qrCodeUtil.createQRCode();//生成二維碼

? ? ? ? //使用對象 讀取二維碼

? ? ? // QrCodeUtil qqq=new QrCodeUtil();

//? ? ? ? String result= qqq.readQrCode();

//? ? ? ? String result= QrCodeUtil.readQrCode("d:/142.png");

? ? ? // System.out.println(result);

? ? }

? ? public String getContent() {

? ? ? ? return content;

? ? }

? ? public void setContent(String content) {

? ? ? ? this.content = content;

? ? }

? ? public String getQrCodeUrl() {

? ? ? ? return qrCodeUrl;

? ? }

? ? public void setQrCodeUrl(String qrCodeUrl) {

? ? ? ? this.qrCodeUrl = qrCodeUrl;

? ? }

? ? public String getFilePath() {

? ? ? ? return filePath;

? ? }

? ? public void setFilePath(String filePath) {

? ? ? ? this.filePath = filePath;

? ? }

? ? public String getFileName() {

? ? ? ? return fileName;

? ? }

? ? public void setFileName(String fileName) {

? ? ? ? this.fileName = fileName;

? ? }

? ? public String getLogoPath() {

? ? ? ? return logoPath;

? ? }

? ? public void setLogoPath(String logoPath) {

? ? ? ? this.logoPath = logoPath;

? ? }

? ? public Integer getWidth() {

? ? ? ? return width;

? ? }

? ? public void setWidth(Integer width) {

? ? ? ? this.width = width;

? ? }

? ? public Integer getHeight() {

? ? ? ? return height;

? ? }

? ? public void setHeight(Integer height) {

? ? ? ? this.height = height;

? ? }

? ? public Integer getOnColor() {

? ? ? ? return onColor;

? ? }

? ? public void setOnColor(Integer onColor) {

? ? ? ? this.onColor = onColor;

? ? }

? ? public Integer getBgColor() {

? ? ? ? return bgColor;

? ? }

? ? public void setBgColor(Integer bgColor) {

? ? ? ? this.bgColor = bgColor;

? ? }

? ? public Integer getMargin() {

? ? ? ? return margin;

? ? }

? ? public void setMargin(Integer margin) {

? ? ? ? this.margin = margin;

? ? }

? ? public ErrorCorrectionLevel getLevel() {

? ? ? ? return level;

? ? }

? ? public void setLevel(ErrorCorrectionLevel level) {

? ? ? ? this.level = level;

? ? }

? ? public String getImageFormat() {

? ? ? ? return imageFormat;

? ? }

? ? public void setImageFormat(String imageFormat) {

? ? ? ? this.imageFormat = imageFormat;

? ? }

}


讀取二維碼

package com.palmble.common.zxing;

import com.google.common.collect.Maps;

import com.google.zxing.*;

import com.google.zxing.client.j2se.BufferedImageLuminanceSource;

import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Map;

public class ReadUtil {

? ? public static? String decodeImg(File file) throws IOException, NotFoundException {

? ? ? ? if(!file.exists()) return "";

? ? ? BufferedImage bufferedImage= ImageIO.read(file);

? ? ? ? MultiFormatReader multiFormatReader= new MultiFormatReader();

? ? ? ? LuminanceSource luminanceSource=new BufferedImageLuminanceSource(bufferedImage);

? ? ? ? Binarizer binarizer=new HybridBinarizer(luminanceSource);

? ? ? ? BinaryBitmap binaryBitmap=new BinaryBitmap(binarizer);

? ? ? ? Map map= Maps.newHashMap();

? ? ? ? map.put(EncodeHintType.CHARACTER_SET,"utf-8");

? ? ? ? Result result=multiFormatReader.decode(binaryBitmap,map);

? ? ? ? return? result.toString();

? ? }

}



測試

ZXCodeUtil

package com.palmble.common.zxing;

import com.google.zxing.BarcodeFormat;

import com.google.zxing.EncodeHintType;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.WriterException;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Hashtable;

import java.util.Map;

public class ZXCodeUtil {

? /* private String content;? ? ? ? ? ? ? ? //二維碼內(nèi)容

? ? private String qrCodeUrl;? ? ? ? ? //二維碼網(wǎng)絡路徑

? ? private String filePath;? ? ? ? ? //二維碼生成物理路徑 也可以是文件的絕對路徑

? ? private String fileName;? ? ? ? ? //二維碼生成圖片名稱(包含后綴名)

? ? private String imageFormat;? ? ? //二維碼圖片后綴名(jpg、png)

? ? private String logoPath;? ? ? ? ? //logo圖片

? ? private Integer width = 300;? ? ? ? ? //二維碼寬度

? ? private Integer height = 300;? ? ? ? ? //二維碼高度*/

? /* private Integer onColor = 0xFF000000;? //前景色 黑色

? ? private Integer bgColor = 0xFFFFFFFF; //背景色 白色

? ? private Integer margin = 2;? ? ? ? ? ? //白邊大小,取值范圍0~4

? ? private ErrorCorrectionLevel level = ErrorCorrectionLevel.M;? //二維碼容錯率指 糾錯級別(L 7%、M 15%、Q 25%、H 30%)

*/

? ? private static? int BLACK = 0xFF000000;//用于設置圖案的顏色

? ? private static? int WHITE = 0xFFFFFFFF; //用于背景色

? ? //加密

? ? /**

? ? * 生成包含字符串信息的二維碼圖片

? ? * @param imgpath 保存文件路徑

? ? * @param formatimge 二維碼圖片格式

? ? * @param content 二維碼攜帶信息

? ? * @param width 二維碼圖片的寬度

? ? * @param height

? ? * @param logopath

? ? * @throws Exception

? ? */

? ? public static void encodeImage(String imgpath,String formatimge,String content,int width,int height,String logopath) throws Exception {

? ? ? ? File file=new File(imgpath);

? ? ? ? if(!file.exists()){

? ? ? ? ? ? throw new Exception("文件路徑不存在");

? ? ? ? }

? ? ? ? Map <EncodeHintType,Object> hints=new Hashtable <>();

? ? ? ? //排錯率 L<M<Q<H

? ? ? ? hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 指定糾錯等級

? ? ? ? hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定編碼格式

? ? ? ? hints.put(EncodeHintType.MARGIN,1);//外邊距

? ? ? ? /**

? ? ? ? * content 加密的內(nèi)容

? ? ? ? * BarcodeFormat.QR_CODE 要解析的類型

? ? ? ? * hints 加密涉及的一些參數(shù):編碼、排錯率

? ? ? ? */

? ? ? ? BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hints);

? ? ? //內(nèi)存中的圖片

? ? ? ? BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

? ? ? ? for (int x=0;x<width;x++){

? ? ? ? ? ? for (int y = 0; y < height; y++) {

? ? ? ? ? ? ? ? /*int[] rgbBlack=new int[]{0,0,0};

? ? ? ? ? ? ? ? int[] rgbWhite=new int[]{255,255,255};*/

? ? ? ? ? ? ? ? bufferedImage.setRGB(x,y,(bitMatrix.get(x,y)?BLACK:WHITE));

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? //logo

? ? ? bufferedImage= LogoUtil.logoMatrix(bufferedImage,logopath);

? ? ? ? //生成二維碼圖片

? ? ? ? ImageIO.write(bufferedImage,formatimge,file);

? ? }

? ? public static void main(String[] args) throws Exception {

? ? ? ? ZXCodeUtil.encodeImage("D:/zxing.gif","gif","在海南的朋友們想買野生土蜂蜜可以找我喲?。。。?,300,300,"D:/1.png");

? ? ? ? ? //File file=new File("D:/2.png");

? ? ? ? ? //String string=ReadUtil.decodeImg(file);

? ? ? //? System.out.println("string=="+string);

? ? ? /* QrCodeUtil qrCodeUtil=new QrCodeUtil();

? ? ? ? qrCodeUtil.setFilePath("D:/zxing2.gif");

? ? ? ? qrCodeUtil.setImageFormat("gif");

? ? ? ? qrCodeUtil.setContent("com");

? ? ? ? qrCodeUtil.setWidth(200);

? ? ? ? qrCodeUtil.setHeight(200);

? ? ? ? qrCodeUtil.setLogoPath("d:/1.png");

? ? ? ? qrCodeUtil.createQRCode();*/

? ? }

? ? }

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容