引用spire.barcode.jar包

創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)驛城,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
//創(chuàng)建BarcodeSettings對(duì)象
BarcodeSettings?settings?=?new?BarcodeSettings();
//設(shè)置條碼類型為
QR二維碼settings.setType(BarCodeType.QR_Code);???????
//設(shè)置二維碼數(shù)據(jù)
settings.setData("Hello?123456789");
//設(shè)置二維碼顯示數(shù)據(jù)
settings.setData2D("Hello?123456789");?????
//設(shè)置數(shù)據(jù)類型
settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);
//設(shè)置二維碼模型寬度
settings.setX(1.0f);
//設(shè)置二維碼糾錯(cuò)級(jí)別settings.setQRCodeECL(QRCodeECL.H);
//創(chuàng)建BarCodeGenerator實(shí)例
BarCodeGenerator?barCodeGenerator?=?new?BarCodeGenerator(settings);
//根據(jù)settings生成圖像數(shù)據(jù),保存至BufferedImage
BufferedImage?bufferedImage?=?barCodeGenerator.generateImage();
//將圖片數(shù)據(jù)保存為PNG格式
ImageIO.write(bufferedImage,?"png",?new?File("QRCode.png"));
在java工程里面導(dǎo)入QRCode.JAR包
import?java.awt.Color;
import?java.awt.Graphics2D;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.OutputStream;
import?javax.imageio.ImageIO;
import?jp.sourceforge.qrcode.QRCodeDecoder;
import?jp.sourceforge.qrcode.exception.DecodingFailedException;
import?com.swetake.util.Qrcode;
public?class?TwoDimensionCode?{
/**
?*?生成二維碼(QRCode)圖片
?*?
?*?@param?content
?*????????????存儲(chǔ)內(nèi)容
?*?@param?imgPath
?*????????????圖片路徑
?*/
public?void?encoderQRCode(String?content,?String?imgPath)?{
this.encoderQRCode(content,?imgPath,?"png",?7);
}
/**
?*?生成二維碼(QRCode)圖片
?*?
?*?@param?content
?*????????????存儲(chǔ)內(nèi)容
?*?@param?output
?*????????????輸出流
?*/
public?void?encoderQRCode(String?content,?OutputStream?output)?{
this.encoderQRCode(content,?output,?"png",?7);
}
/**
?*?生成二維碼(QRCode)圖片
?*?
?*?@param?content
?*????????????存儲(chǔ)內(nèi)容
?*?@param?imgPath
?*????????????圖片路徑
?*?@param?imgType
?*????????????圖片類型
?*/
public?void?encoderQRCode(String?content,?String?imgPath,?String?imgType)?{
this.encoderQRCode(content,?imgPath,?imgType,?7);
}
/**
?*?生成二維碼(QRCode)圖片
?*?
?*?@param?content
?*????????????存儲(chǔ)內(nèi)容
?*?@param?output
?*????????????輸出流
?*?@param?imgType
?*????????????圖片類型
?*/
public?void?encoderQRCode(String?content,?OutputStream?output,
String?imgType)?{
this.encoderQRCode(content,?output,?imgType,?7);
}
/**
?*?生成二維碼(QRCode)圖片
?*?
?*?@param?content
?*????????????存儲(chǔ)內(nèi)容
?*?@param?imgPath
?*????????????圖片路徑
?*?@param?imgType
?*????????????圖片類型
?*?@param?size
?*????????????二維碼尺寸
?*/
public?void?encoderQRCode(String?content,?String?imgPath,?String?imgType,
int?size)?{
try?{
BufferedImage?bufImg?=?this.qRCodeCommon(content,?imgType,?size);
File?imgFile?=?new?File(imgPath);
//?生成二維碼QRCode圖片
ImageIO.write(bufImg,?imgType,?imgFile);
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
/**
?*?生成二維碼(QRCode)圖片
?*?
?*?@param?content
?*????????????存儲(chǔ)內(nèi)容
?*?@param?output
?*????????????輸出流
?*?@param?imgType
?*????????????圖片類型
?*?@param?size
?*????????????二維碼尺寸
?*/
public?void?encoderQRCode(String?content,?OutputStream?output,
String?imgType,?int?size)?{
try?{
BufferedImage?bufImg?=?this.qRCodeCommon(content,?imgType,?size);
//?生成二維碼QRCode圖片
ImageIO.write(bufImg,?imgType,?output);
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
/**
?*?生成二維碼(QRCode)圖片的公共方法
?*?
?*?@param?content
?*????????????存儲(chǔ)內(nèi)容
?*?@param?imgType
?*????????????圖片類型
?*?@param?size
?*????????????二維碼尺寸
?*?@return
?*/
private?BufferedImage?qRCodeCommon(String?content,?String?imgType,?int?size)?{
BufferedImage?bufImg?=?null;
try?{
Qrcode?qrcodeHandler?=?new?Qrcode();
//?設(shè)置二維碼排錯(cuò)率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯(cuò)率越高可存儲(chǔ)的信息越少,但對(duì)二維碼清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
//?設(shè)置設(shè)置二維碼尺寸,取值范圍1-40,值越大尺寸越大,可存儲(chǔ)的信息越大
qrcodeHandler.setQrcodeVersion(size);
//?獲得內(nèi)容的字節(jié)數(shù)組,設(shè)置編碼格式
byte[]?contentBytes?=?content.getBytes("utf-8");
//?圖片尺寸
int?imgSize?=?67?+?12?*?(size?-?1);
bufImg?=?new?BufferedImage(imgSize,?imgSize,
BufferedImage.TYPE_INT_RGB);
Graphics2D?gs?=?bufImg.createGraphics();
//?設(shè)置背景顏色
gs.setBackground(Color.WHITE);
gs.clearRect(0,?0,?imgSize,?imgSize);
//?設(shè)定圖像顏色?BLACK
gs.setColor(Color.BLACK);
//?設(shè)置偏移量,不設(shè)置可能導(dǎo)致解析出錯(cuò)
int?pixoff?=?2;
//?輸出內(nèi)容?二維碼
if?(contentBytes.length??0??contentBytes.length??800)?{
boolean[][]?codeOut?=?qrcodeHandler.calQrcode(contentBytes);
for?(int?i?=?0;?i??codeOut.length;?i++)?{
for?(int?j?=?0;?j??codeOut.length;?j++)?{
if?(codeOut[j][i])?{
gs.fillRect(j?*?3?+?pixoff,?i?*?3?+?pixoff,?3,?3);
}
}
}
}?else?{
throw?new?Exception("QRCode?content?bytes?length?=?"
+?contentBytes.length?+?"?not?in?[0,?800].");
}
gs.dispose();
bufImg.flush();
}?catch?(Exception?e)?{
e.printStackTrace();
}
return?bufImg;
}
/**?
*?解析二維碼(QRCode)?
*?@param?imgPath?圖片路徑?
*?@return?
*/??
public?String?decoderQRCode(String?imgPath)?{??
//?QRCode?二維碼圖片的文件??
File?imageFile?=?new?File(imgPath);??
BufferedImage?bufImg?=?null;??
String?content?=?null;??
try?{??
bufImg?=?ImageIO.read(imageFile);??
QRCodeDecoder?decoder?=?new?QRCodeDecoder();??
content?=?new?String(decoder.decode(new?TwoDimensionCodeImage(bufImg)),?"utf-8");???
}?catch?(IOException?e)?{??
System.out.println("Error:?"?+?e.getMessage());??
e.printStackTrace();??
}?catch?(DecodingFailedException?dfe)?{??
System.out.println("Error:?"?+?dfe.getMessage());??
dfe.printStackTrace();??
}??
return?content;??
}??
/**?
*?解析二維碼(QRCode)?
*?@param?input?輸入流?
*?@return?
*/??
public?String?decoderQRCode(InputStream?input)?{??
BufferedImage?bufImg?=?null;??
String?content?=?null;??
try?{??
bufImg?=?ImageIO.read(input);??
QRCodeDecoder?decoder?=?new?QRCodeDecoder();??
content?=?new?String(decoder.decode(new?TwoDimensionCodeImage(bufImg)),?"utf-8");???
}?catch?(IOException?e)?{??
System.out.println("Error:?"?+?e.getMessage());??
e.printStackTrace();??
}?catch?(DecodingFailedException?dfe)?{??
System.out.println("Error:?"?+?dfe.getMessage());??
dfe.printStackTrace();??
}??
return?content;??
}??
public?static?void?main(String[]?args)?{??
String?imgPath?=?"G:/TDDOWNLOAD/Michael_QRCode.png";??
String?encoderContent?=?"Hello?大大、小小,welcome?to?QRCode!"?+?"\nMyblog?[??]"?+?"\nEMail?[?sjsky007@gmail.com?]";??
TwoDimensionCode?handler?=?new?TwoDimensionCode();??
handler.encoderQRCode(encoderContent,?imgPath,?"png");??
//??????try?{??
//??????????OutputStream?output?=?new?FileOutputStream(imgPath);??
//??????????handler.encoderQRCode(content,?output);??
//??????}?catch?(Exception?e)?{??
//??????????e.printStackTrace();??
//??????}??
System.out.println("========encoder?success");??
String?decoderContent?=?handler.decoderQRCode(imgPath);??
System.out.println("解析結(jié)果如下:");??
System.out.println(decoderContent);??
System.out.println("========decoder?success!!!");??
}??
}
第二個(gè)代碼:
import?java.awt.image.BufferedImage;??
import?jp.sourceforge.qrcode.data.QRCodeImage;??
public?class?TwoDimensionCodeImage?implements?QRCodeImage?{??
BufferedImage?bufImg;??
public?TwoDimensionCodeImage(BufferedImage?bufImg)?{??
this.bufImg?=?bufImg;??
}??
@Override??
public?int?getHeight()?{??
return?bufImg.getHeight();??
}??
@Override??
public?int?getPixel(int?x,?int?y)?{??
return?bufImg.getRGB(x,?y);??
}??
@Override??
public?int?getWidth()?{??
return?bufImg.getWidth();??
}??
}
參考:
import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class CreateDataMatrix {
public static void main(String[] args) throws Exception {
? //生成BarcodeSettings實(shí)例
? BarcodeSettings settings = new BarcodeSettings();
? //設(shè)置條形碼類型為DataMatrix
? settings.setType(BarCodeType.Data_Matrix);
? //設(shè)置條形碼模型寬度
? settings.setX(1.5f);
? //設(shè)置數(shù)據(jù)和顯示文本
? settings.setData("ABC 123456789ABC 123456789ABC 123456789");
? settings.setData2D("ABC 123456789ABC 123456789ABC 123456789");
? //創(chuàng)建BarCodeGenerator實(shí)例
? BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
? //根據(jù)settings生成圖像數(shù)據(jù),保存至BufferedImage實(shí)例
? BufferedImage bufferedImage = barCodeGenerator.generateImage();
? //保存為PNG圖片
? ImageIO.write(bufferedImage, "png", new File("DataMatrix.png"));
? System.out.println("Complete!");
}
}
用到了spire.barcode for java庫
1: 使用SwetakeQRCode在Java項(xiàng)目中生成二維碼
下載地址
或著
這個(gè)是日本人寫的,生成的是我們常見的方形的二維碼
可以用中文
如:5677777ghjjjjj
2: 使用BarCode4j生成條形碼和二維碼
BarCode4j網(wǎng)址:
barcode4j是使用datamatrix的二維碼生成算法,為支持qr的算法
datamatrix是歐美的標(biāo)準(zhǔn),qr為日本的標(biāo)準(zhǔn),
barcode4j一般生成出來是長方形的
如:88777alec000yan
這個(gè)博客這方面說的挺清楚的:
3:zxing
zxing 這個(gè)是google的
下載地址
Java代碼:
import java.io.File;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class QRCodeEvents {
public static void main(String []args)throws Exception{
String text = "你好";
int width = 100;
int height = 100;
String format = "png";
Hashtable hints= new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);
File outputFile = new File("new.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
}
}
4:google chart api就有實(shí)現(xiàn)二維碼的方法
利用這個(gè)api,使用google appengine進(jìn)行實(shí)現(xiàn)。
5:JS生成二維碼
使用jQuery-qrcode生成二維碼
先簡單說一下jquery-qrcode,這個(gè)開源的三方庫(可以從 獲取),
qrcode.js 是實(shí)現(xiàn)二維碼數(shù)據(jù)計(jì)算的核心類,
jquery.qrcode.js 是把它用jquery方式封裝起來的,用它來實(shí)現(xiàn)圖形渲染,其實(shí)就是畫圖(支持canvas和table兩種方式)
支持的功能主要有:
Js代碼:
text : "" //設(shè)置二維碼內(nèi)容
Js代碼:
render : "canvas",//設(shè)置渲染方式
width : 256, //設(shè)置寬度
height : 256, //設(shè)置高度
typeNumber : -1, //計(jì)算模式
correctLevel : QRErrorCorrectLevel.H,//糾錯(cuò)等級(jí)
background : "#ffffff",//背景顏色
foreground : "#000000" //前景顏色
使用方式非常簡單
Js代碼:
jQuery('#output').qrcode({width:200,height:200,correctLevel:0,text:content});
經(jīng)過簡單實(shí)踐,
使用canvas方式渲染性能還是非常不錯(cuò)的,但是如果用table方式,性能不太理想,特別是IE9以下的瀏覽器,所以需要自行優(yōu)化一下渲染table的方式,這里就不細(xì)述了。
其實(shí)上面的js有一個(gè)小小的缺點(diǎn),就是默認(rèn)不支持中文。
這跟js的機(jī)制有關(guān)系,jquery-qrcode這個(gè)庫是采用 charCodeAt() 這個(gè)方式進(jìn)行編碼轉(zhuǎn)換的,
而這個(gè)方法默認(rèn)會(huì)獲取它的 Unicode 編碼,一般的解碼器都是采用UTF-8, ISO-8859-1等方式,
英文是沒有問題,如果是中文,一般情況下Unicode是UTF-16實(shí)現(xiàn),長度2位,而UTF-8編碼是3位,這樣二維碼的編解碼就不匹配了。
解決方式當(dāng)然是,在二維碼編碼前把字符串轉(zhuǎn)換成UTF-8,具體代碼如下:
function utf16to8(str) {
var out, i, len, c;
out = "";
len = str.length;
for(i = 0; i len; i++) {
c = str.charCodeAt(i);
if ((c = 0x0001) (c = 0x007F)) {
out += str.charAt(i);
} else if (c 0x07FF) {
out += String.fromCharCode(0xE0 | ((c 12) 0x0F));
out += String.fromCharCode(0x80 | ((c 6) 0x3F));
out += String.fromCharCode(0x80 | ((c 0) 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c 6) 0x1F));
out += String.fromCharCode(0x80 | ((c 0) 0x3F));
}
}
return out;
}
標(biāo)題名稱:二維碼支付java代碼 二維碼支付java代碼是什么
本文路徑:http://www.chinadenli.net/article0/dddhdio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站設(shè)計(jì)、App開發(fā)、網(wǎng)站制作、品牌網(wǎng)站制作、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)