-
查看全部
-
生成:
package com.jz.qrcode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
/**
?* 使用QRCode來生成二維碼
?* 生成原理:根據(jù)java GUI畫圖工具來生成
?*?
?* */
public class CreatQRCode {
public static void main(String[] args) {
String qrData = "www.baidu.com";
Qrcode x = new Qrcode();
x.setQrcodeErrorCorrect('M');//糾錯等級
x.setQrcodeEncodeMode('B');//N 數(shù)字? ?A a-z? B代表其他內容
/*
* 版本? ?1-40
* 從21x21(版本1),到177x177(版本40),每一版本符號比前一版本每邊增加4個模塊。
* */
int version = 7;
x.setQrcodeVersion(version);
//畫的長度根據(jù)版本的不同,大小不同, 下面的長度計算公式固定
int width = 67+12*(version-1);
int height = 67+12*(version-1);
//BufferedImage.TYPE_INT_RGB? ? 指定圖片的RGB 值為int型 的 8位
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
/*
* Graphics 繪圖
*?
* */
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);
int pixff = 2;//偏移量
byte[] d =qrData.getBytes();
if (d.length>0 && d.length <120){
? ? boolean[][] s = x.calQrcode(d);
? ? for (int i=0;i<s.length;i++){
for (int j=0;j<s.length;j++){
? ? if (s[j][i]) {
gs.fillRect(j*3+pixff,i*3+pixff,3,3);
? ? }
}
? ? }
}
gs.dispose();
bufferedImage.flush();
try {
ImageIO.write(bufferedImage, "png", new File("D:/baidu2_QRCode.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
識別:
package com.jz.qrcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
/**
?* QRCode 解析二維碼信息
?*?
?* */
public class ReaderQRCode {
public static void main(String[] args) {
File file = new File("D:/baidu2_QRCode.png");
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(file);
QRCodeDecoder codeDecoder = new QRCodeDecoder();
String result = new String(codeDecoder.decode(new MyQRCodeImage(
bufferedImage)),"gb2312");
System.out.println(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
接口:
package com.jz.qrcode;
import java.awt.image.BufferedImage;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class MyQRCodeImage implements QRCodeImage {
BufferedImage bufferedImage;
public MyQRCodeImage(BufferedImage bufferedImage){
this.bufferedImage = bufferedImage;
}
@Override
public int getHeight() {
return bufferedImage.getHeight();
}
@Override
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
@Override
public int getWidth() {
return bufferedImage.getWidth();
}
}
查看全部 -
查看全部
-
package com.jz.QRCode;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
public class ReaderQRCode {
public static void main(String[] args) {
try {
/*
* MultiFormatReader 多格式讀取
*?
* */
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("D:/baidu_QRCode.png");
//讀取圖片buffer中
BufferedImage bufferedImage = ImageIO.read(file);
/*
* BinaryBitmap 二進制位圖
* HybridBinarizer 混合二值化器
* BufferedImageLuminanceSource? ?圖像緩存區(qū) 亮度 資源
*?
* */
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
//定義二維碼參數(shù)
HashMap hashMap = new HashMap();
hashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");//編碼方式
Result result = formatReader.decode(binaryBitmap,hashMap);
System.out.println("解析結果:"+result.toString());
System.out.println("二維碼格式類型:"+result.getBarcodeFormat());//BarcodeFormat? ?條形碼格式
System.out.println("二維碼文本內容:"+result.getText());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
查看全部 -
/** *使用zxing生成二維碼 */?
public class CreateQRCode {?
?public static void main(String[] arge) throws WriterException, IOException{
?int width = 300;//定義圖片寬度 int height = 300;//定義圖片高度 String format ="png";//定義圖片格式 String content ="www.8664600.com";//定義二維碼內容 //定義二維碼參數(shù) HashMap hints = ?new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//設置編碼 //設置容錯等級,等級越高,容量越小 hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN,2);//設置邊距 //生成矩陣 BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, width, height); //設置路徑 Path file = new File("C:/Users/qqazl001/Desktop/Rui/ma.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, file);//輸出圖像 } }
查看全部 -
Zxing:
? 網(wǎng)址:https://github.com/zxing/
? 特點:開源 java編寫
? 下載后為源代碼,需要自己打成jar包(包含core中的 com和javase中的com)
查看全部 -
糾錯能力越高,承載信息越少
查看全部 -
二維碼的分類
查看全部 -
QR 二維碼
查看全部 -
二維碼查看全部
-
生成
查看全部 -
QR Code
查看全部 -
QR code
查看全部 -
缺點
查看全部 -
優(yōu)點
查看全部
舉報