課程
/后端開發
/Java
/Java生成二維碼
d.length < 120 只能輸入14個字符長度信息的內容,太小了,將120修改之后,就會報錯java.lang.ArrayIndexOutOfBoundsException,這個怎么解決?
2017-04-25
源自:Java生成二維碼 6-4
正在回答
package com.wang.test;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
/**
?* 生成二維碼
?* @author Developer Name
?*
?*/
public class Test3 {
public static void main(String[] args) throws Exception {
String content = "ART M20327,#115664742,9TXZFHPB00185";
String outPath = "D:\\qrCode\\code.jpg";
String logoPath = "D:\\qrCode\\logo.png";
creatQrCode(1, 0, content, outPath, logoPath);
}
* 生成二維碼
* @param version ? 版本
* @param content ? 內容
* @param outPath ? 輸出路徑
* @param logoPath ?圖標路徑
* @param pixoff ? ?偏移量
* @throws Exception
*/
public static void creatQrCode(int version, ?int pixoff, String content, String outPath,
String logoPath) throws Exception {
// 圖片尺寸
int width = 67 + 12 * (version - 1);
int height = 67 + 12 * (version - 1);
//int width = 30;
//int height = 30;
// 設置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小
Qrcode code = new Qrcode();
code.setQrcodeErrorCorrect('M');
code.setQrcodeEncodeMode('B');
// 設置設置二維碼尺寸,取值范圍1-40,值越大尺寸越大,可存儲的信息越大
code.setQrcodeVersion(version);
BufferedImage bufferImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufferImage.createGraphics();
// 設置背景顏色
gs.setBackground(Color.WHITE);
// 設定圖像顏色> BLACK
gs.setColor(Color.BLACK);
//清空畫板
gs.clearRect(0, 0, width, height);
// 獲得內容的字節數組,設置編碼格式
byte[] contentBytes = content.getBytes("gb2312");
// 輸出內容> 二維碼
if (contentBytes.length > 0 && contentBytes.length < 120) {
boolean[][] codeOut = code.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].");
createPhotoAtCenter(bufferImage, logoPath);
gs.dispose();
bufferImage.flush();
ImageIO.write(bufferImage, "jpg", new File(outPath));
* 加入logo
*?
* @param bufImg ? ? 要添加的二維碼對象
* @param logoPath ? 要添加logo的路徑
* @return
private static BufferedImage createPhotoAtCenter(BufferedImage bufImg, String logoPath) throws Exception {
Image image = ImageIO.read(new File(logoPath));
Graphics2D graphics = bufImg.createGraphics();
// 獲取bufImg的中間位置
int centerX = bufImg.getMinX() + bufImg.getWidth() / 2 - 30 / 2;
int centerY = bufImg.getMinY() + bufImg.getHeight() / 2 - 30 / 2;
graphics.drawImage(image, centerX + 35, centerY, 30, 30, null);
graphics.dispose();
bufImg.flush();
return bufImg;
少導個包import java.io.File;
debug一下看看或把代碼都發給我
慕粉1523472586 提問者
舉報
二維碼無處不在,自己動手用Java生成二維碼,三種生成方式任你選
3 回答關于本堂課程 創造QR Code長度和高度
1 回答我的put會報錯是為什么
3 回答qrcode解析出來是一長串數字??求解
1 回答為什么我的會報錯誤文件路徑找不到?
5 回答Path報錯
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-05-24
package com.wang.test;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
/**
?* 生成二維碼
?* @author Developer Name
?*
?*/
public class Test3 {
public static void main(String[] args) throws Exception {
String content = "ART M20327,#115664742,9TXZFHPB00185";
String outPath = "D:\\qrCode\\code.jpg";
String logoPath = "D:\\qrCode\\logo.png";
creatQrCode(1, 0, content, outPath, logoPath);
}
/**
* 生成二維碼
* @param version ? 版本
* @param content ? 內容
* @param outPath ? 輸出路徑
* @param logoPath ?圖標路徑
* @param pixoff ? ?偏移量
* @throws Exception
*/
public static void creatQrCode(int version, ?int pixoff, String content, String outPath,
String logoPath) throws Exception {
// 圖片尺寸
int width = 67 + 12 * (version - 1);
int height = 67 + 12 * (version - 1);
//int width = 30;
//int height = 30;
// 設置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小
Qrcode code = new Qrcode();
code.setQrcodeErrorCorrect('M');
code.setQrcodeEncodeMode('B');
// 設置設置二維碼尺寸,取值范圍1-40,值越大尺寸越大,可存儲的信息越大
code.setQrcodeVersion(version);
BufferedImage bufferImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufferImage.createGraphics();
// 設置背景顏色
gs.setBackground(Color.WHITE);
// 設定圖像顏色> BLACK
gs.setColor(Color.BLACK);
//清空畫板
gs.clearRect(0, 0, width, height);
// 獲得內容的字節數組,設置編碼格式
byte[] contentBytes = content.getBytes("gb2312");
// 輸出內容> 二維碼
if (contentBytes.length > 0 && contentBytes.length < 120) {
boolean[][] codeOut = code.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].");
}
createPhotoAtCenter(bufferImage, logoPath);
gs.dispose();
bufferImage.flush();
ImageIO.write(bufferImage, "jpg", new File(outPath));
}
/**
* 加入logo
*?
* @param bufImg ? ? 要添加的二維碼對象
* @param logoPath ? 要添加logo的路徑
* @return
* @throws Exception
*/
private static BufferedImage createPhotoAtCenter(BufferedImage bufImg, String logoPath) throws Exception {
Image image = ImageIO.read(new File(logoPath));
Graphics2D graphics = bufImg.createGraphics();
// 獲取bufImg的中間位置
int centerX = bufImg.getMinX() + bufImg.getWidth() / 2 - 30 / 2;
int centerY = bufImg.getMinY() + bufImg.getHeight() / 2 - 30 / 2;
graphics.drawImage(image, centerX + 35, centerY, 30, 30, null);
graphics.dispose();
bufImg.flush();
return bufImg;
}
}
2017-05-10
少導個包import java.io.File;
debug一下看看或把代碼都發給我