二維碼分為好多種,我們最常用的是qrcode類型的二維碼,以下有三種生成方式以及解析方式:
附所需jar包或者js地址
第一種:依賴qrcode.jar
-
import java.awt.Color;
-
import java.awt.Graphics2D;
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.UnsupportedEncodingException;
-
-
import javax.imageio.ImageIO;
-
-
import com.swetake.util.Qrcode;
-
/**獲取qrcode jar包 https://osdn.jp/projects/qrcode
-
* 生成qrcode二維碼
-
* @author fangyi
-
*
-
*/
-
public
class CreateQrCode {
-
public static void main(String[] args) throws Exception {
-
Qrcode qrcode=
new Qrcode();
-
qrcode.setQrcodeErrorCorrect(
'M');
//糾錯等級
-
qrcode.setQrcodeEncodeMode(
'B');
//N代表數字 A代表字母 B代表其他
-
qrcode.setQrcodeVersion(
7);
//版本
-
String qrData =
"www.baidu.com";
//二維碼信息內容
-
int width=
67+
12*(
7-
1);
-
int height=
67+
12*(
7-
1);
-
-
BufferedImage bufferedImage =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-
//畫圖工具
-
Graphics2D gs= bufferedImage.createGraphics();
-
//設置屬性
-
gs.setBackground(Color.WHITE);
-
gs.setColor(Color.BLACK);
-
gs.clearRect(
0,
0, width, height);
-
//填充
-
int pixoff=
2;
//偏移量
-
byte[] d = qrData.getBytes(
"gb2312");
//編碼防止有中文
-
if (
0 < d.length && d.length<
120) {
-
boolean[][] s = qrcode.calQrcode(d);
-
for (
int i =
0; i < s.length; i++) {
-
for (
int j =
0; j < s.length; j++) {
-
if(s[j][i]){
-
gs.fillRect(i*
3+pixoff, j*
3+pixoff,
3,
3);
-
}
-
}
-
}
-
}
-
//關閉資源,並保存文件
-
gs.dispose();
-
bufferedImage.flush();
-
ImageIO.write(bufferedImage,
"png",
new File(
"D:/qrcode.jpg"));
//輸出到指定地方
-
}
-
}
讀取指定的二維碼:
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.IOException;
-
-
import javax.imageio.ImageIO;
-
-
import jp.sourceforge.qrcode.QRCodeDecoder;
-
/**
-
* 讀取制定路徑下的qrcode信息
-
* @author fangyi
-
*
-
*/
-
public
class ReaderQrcode {
-
-
public static void main(String[] args) throws IOException {
-
//指定的二維碼文件路徑
-
File file=
new File(
"D:/jqueryqrcode.png");
-
BufferedImage bufferedImage = ImageIO.read(file);
-
QRCodeDecoder qrCodeDecoder =
new QRCodeDecoder();
-
String result =
new String(qrCodeDecoder.decode(
new MyQRCodeImage(bufferedImage)),
"gb2312");
-
//輸出結果
-
System.out.println(result);
-
bufferedImage.flush();
-
}
-
}
-
-
-
import java.awt.image.BufferedImage;
-
-
import jp.sourceforge.qrcode.data.QRCodeImage;
-
-
/**
-
* 自定義QRCodeImage類實現QRCodeImage 供讀取時所用
-
* @author fangyi
-
*
-
*/
-
public
class MyQRCodeImage implements QRCodeImage {
-
-
BufferedImage bufferedImage ;
-
-
public MyQRCodeImage(BufferedImage bufferedImage) {
-
super();
-
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();
-
}
-
-
}
第二種:創建動態的web工程 依賴jquery.qrcode.min.js jquery.js 運行工程訪問下面jsp就可以了
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
-
pageEncoding=
"UTF-8"
%>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-
<title>
生成二維碼
</title>
-
<!-- js地址https://github.com/jeromeetienne/jquery-qrcode -->
-
<script type="text/javascript" src="
<%=request.getContextPath() %>
/js/jquery.min.js"></script>
-
<script type="text/javascript" src="
<%=request.getContextPath() %>
/js/jquery.qrcode.min.js"></script>
-
</head>
-
<body>
-
生成二維碼如下:
<br>
<br>
<br>
-
<div id="qrcode">
</div>
-
-
<script type="text/javascript">
-
$(
'#qrcode'
).qrcode(
"www.baidu.com"
);
//生產二維碼
-
/* jquery('#qrcode').qrcode({width: 64,height: 64,text: "size doesn't matter"}); */
-
</script>
-
-
</body>
-
</html>
第三種 創建Java project 依賴zxing3.2.1.jar
-
import java.io.File;
-
import java.io.IOException;
-
import java.util.Hashtable;
-
-
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;
-
/**
-
* zxing jar包地址 https://github.com/Candy1575/QRcode
-
* 依賴MatrixToImageWriter類
-
* @author fangyi
-
*
-
*/
-
public
class EncodeTest {
-
-
public static void Encode_QR_CODE() throws IOException, WriterException{
-
String contents =
"ZXing 二維碼內容1234!";
// 二維碼內容
-
int width =
430;
// 二維碼圖片寬度 300
-
int height =
430;
// 二維碼圖片高度300
-
-
String format =
"gif";
// 二維碼的圖片格式 gif
-
-
Hashtable<EncodeHintType, Object> hints =
new Hashtable<EncodeHintType, Object>();
-
// 指定糾錯等級,糾錯級別(L 7%、M 15%、Q 25%、H 30%)
-
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
-
// 內容所使用字符集編碼
-
hints.put(EncodeHintType.CHARACTER_SET,
"utf-8");
-
// hints.put(EncodeHintType.MAX_SIZE, 350);//設置圖片的最大值
-
// hints.put(EncodeHintType.MIN_SIZE, 100);//設置圖片的最小值
-
hints.put(EncodeHintType.MARGIN,
1);
//設置二維碼邊的空度,非負數
-
-
BitMatrix bitMatrix =
new MultiFormatWriter().encode(contents,
//要編碼的內容
-
//編碼類型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,
-
//Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,
-
//MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSION
-
BarcodeFormat.QR_CODE,
-
width,
//條形碼的寬度
-
height,
//條形碼的高度
-
hints);
//生成條形碼時的一些配置,此項可選
-
-
// 生成二維碼
-
File outputFile =
new File(
"D:" + File.separator +
"mycode.gif");
//指定輸出路徑
-
-
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
-
}
-
-
public static void main(String[] args) throws Exception {
-
try {
-
Encode_QR_CODE();
-
}
catch (Exception e) {
-
// TODO: handle exception
-
e.printStackTrace();
-
}
-
}
-
-
}
-
-
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.IOException;
-
import java.io.OutputStream;
-
-
import javax.imageio.ImageIO;
-
-
import com.google.zxing.common.BitMatrix;
-
-
/**
-
* 二維碼的生成需要借助MatrixToImageWriter類,
-
* 該類是由Google提供的,可以將該類直接拷貝到源碼中使用,當然你也可以自己寫個
-
* 生產條形碼的基類 依賴LogoConfig類
-
*/
-
public
class MatrixToImageWriter {
-
private
static
final
int BLACK =
0xFF000000;
//用於設置圖案的顏色
-
private
static
final
int WHITE =
0xFFFFFFFF;
//用於背景色
-
-
private MatrixToImageWriter() {
-
}
-
-
public 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));
-
// image.setRGB(x, y, (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB()));
-
}
-
}
-
return image;
-
}
-
-
public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
-
BufferedImage image = toBufferedImage(matrix);
-
//設置logo圖標
-
LogoConfig logoConfig =
new LogoConfig();
-
image = logoConfig.LogoMatrix(image);
-
-
if (!ImageIO.write(image, format, file)) {
-
throw
new IOException(
"Could not write an image of format " + format +
" to " + file);
-
}
else{
-
System.out.println(
"圖片生成成功!");
-
}
-
}
-
-
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
-
BufferedImage image = toBufferedImage(matrix);
-
//設置logo圖標
-
LogoConfig logoConfig =
new LogoConfig();
-
image = logoConfig.LogoMatrix(image);
-
-
if (!ImageIO.write(image, format, stream)) {
-
throw
new IOException(
"Could not write an image of format " + format);
-
}
-
}
-
}
-
-
-
-
import java.awt.BasicStroke;
-
import java.awt.Color;
-
import java.awt.Graphics2D;
-
import java.awt.geom.RoundRectangle2D;
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.IOException;
-
import javax.imageio.ImageIO;
-
/**
-
* 二維碼 添加自定義 logo圖標 處理的方法,
-
* 模仿微信自動生成二維碼效果,有圓角邊框,logo和二維碼間有空白區,logo帶有灰色邊框
-
* @author fangyi
-
*
-
*/
-
public
class LogoConfig {
-
-
/**
-
* 設置 logo
-
* @param matrixImage 源二維碼圖片
-
* @return 返回帶有logo的二維碼圖片
-
* @throws IOException
-
* @author Administrator sangwenhao
-
*/
-
public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException{
-
/**
-
* 讀取二維碼圖片,並構建繪圖對象
-
*/
-
Graphics2D g2 = matrixImage.createGraphics();
-
-
int matrixWidth = matrixImage.getWidth();
-
int matrixHeigh = matrixImage.getHeight();
-
-
/**
-
* 讀取Logo圖片
-
*/
-
BufferedImage logo = ImageIO.read(
new File(
"C:\\Users\\fangyi\\Pictures\\code-wallpaper-18.png"));
-
-
//開始繪制圖片
-
g2.drawImage(logo,matrixWidth/
5*
2,matrixHeigh/
5*
2, matrixWidth/
5, matrixHeigh/
5,
null);
//繪制
-
BasicStroke stroke =
new BasicStroke(
5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
-
g2.setStroke(stroke);
// 設置筆畫對象
-
//指定弧度的圓角矩形
-
RoundRectangle2D.Float round =
new RoundRectangle2D.Float(matrixWidth/
5*
2, matrixHeigh/
5*
2, matrixWidth/
5, matrixHeigh/
5,
20,
20);
-
g2.setColor(Color.white);
-
g2.draw(round);
// 繪制圓弧矩形
-
-
//設置logo 有一道灰色邊框
-
BasicStroke stroke2 =
new BasicStroke(
1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
-
g2.setStroke(stroke2);
// 設置筆畫對象
-
RoundRectangle2D.Float round2 =
new RoundRectangle2D.Float(matrixWidth/
5*
2+
2, matrixHeigh/
5*
2+
2, matrixWidth/
5-
4, matrixHeigh/
5-
4,
20,
20);
-
g2.setColor(
new Color(
128,
128,
128));
-
g2.draw(round2);
// 繪制圓弧矩形
-
-
g2.dispose();
-
matrixImage.flush() ;
-
return matrixImage ;
-
}
-
-
}
讀取二維碼信息
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.IOException;
-
import java.util.HashMap;
-
import java.util.Map;
-
import javax.imageio.ImageIO;
-
import com.google.zxing.Binarizer;
-
import com.google.zxing.BinaryBitmap;
-
import com.google.zxing.EncodeHintType;
-
import com.google.zxing.LuminanceSource;
-
import com.google.zxing.MultiFormatReader;
-
import com.google.zxing.NotFoundException;
-
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;
-
-
/**
-
* 讀取zxing類型下的二維碼信息
-
* @author fangyi
-
*
-
*/
-
public
class ReadQrCode_Zxing {
-
-
@SuppressWarnings(
"unchecked")
-
public static void main(String[] args) {
-
try {
-
MultiFormatReader formatReader =
new MultiFormatReader();
-
//讀取指定的二維碼文件
-
File file=
new File(
"D:\\mycode.gif");
-
BufferedImage bufferedImage =ImageIO.read(file);
-
BinaryBitmap binaryBitmap=
new BinaryBitmap(
new HybridBinarizer(
new BufferedImageLuminanceSource(bufferedImage)));
-
//定義二維碼參數
-
Map hints=
new HashMap<>();
-
hints.put(EncodeHintType.CHARACTER_SET,
"utf-8");
-
Result result = formatReader.decode(binaryBitmap, hints);
-
//輸出相關的二維碼信息
-
System.out.println(
"解析結果"+result.toString());
-
System.out.println(
"二維碼格式類型"+result.getBarcodeFormat());
-
System.out.println(
"二維碼文本內容"+result.getText());
-
bufferedImage.flush();
-
}
catch (NotFoundException e) {
-
e.printStackTrace();
-
}
catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
用二維碼進行添加好友獲取好友信息是用到VCard
微信掃碼登陸網頁原理:通過堵塞等待的長連接,近乎實時的獲得信息。 對於驗證過程,Open API 一般是通過授權令牌(Token)來解決的,原理是當用戶通過授權后,分配一個限定條件下的令牌(如限制本機訪問、限制授權有效時間、限制同時登錄設備數等),使獲得授權的用戶僅在有限的前提下能訪問相關服務。 像計算機休眠后曾做的授權就自動收回了,這樣就有效的避免了在別人電腦上(尤其是網吧)打開,但忘記關閉或退出這類安全問題了。
實現可借鑒已有博客:https://www.cnblogs.com/zijun/p/3580125.html