導航:首頁 > 編程語言 > javaqrcode

javaqrcode

發布時間:2025-05-25 23:57:36

java生成二維碼如何去除白邊

代碼如下:
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = new QRCodeWriter().encode("生成二維碼的內容",
BarcodeFormat.QR_CODE, 256, 256,hints);
//1.1去白邊
int[] rec = bitMatrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (bitMatrix.get(i + rec[0], j + rec[1])) {
resMatrix.set(i, j);
}
}
}
//2
int width = resMatrix.getWidth();
int height = resMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, resMatrix.get(x, y) == true ?
Color.BLACK.getRGB():Color.WHITE.getRGB());
}
}
//3
ImageIO.write(image,"png", new File("生成二維碼保存的路徑"));

2
這時候生成的二維碼圖片就沒有四周的白邊了。

❷ java怎麼生成帶logo二維碼

1、下載生成二維碼所需要的jar包qrcode.jar;
2、直接上生成二維碼的java代碼

//需要導入的包
importjava.awt.Color;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjavax.imageio.ImageIO;
importcom.swetake.util.Qrcode;

/**
*生成二維碼(QRCode)圖片
*@paramcontent二維碼圖片的內容
*@paramimgPath生成二維碼圖片完整的路徑
*@paramccbpath二維碼圖片中間的logo路徑
*/
publicstaticintcreateQRCode(Stringcontent,StringimgPath,StringccbPath){
try{
QrcodeqrcodeHandler=newQrcode();
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(7);

//System.out.println(content);
byte[]contentBytes=content.getBytes("gb2312");
//構造一個BufferedImage對象設置寬、高
BufferedImagebufImg=newBufferedImage(140,140,BufferedImage.TYPE_INT_RGB);
Graphics2Dgs=bufImg.createGraphics();

gs.setBackground(Color.WHITE);
gs.clearRect(0,0,140,140);

//設定圖像顏色>BLACK
gs.setColor(Color.BLACK);

//設置偏移量不設置可能導致解析出錯
intpixoff=2;
//輸出內容>二維碼
if(contentBytes.length>0&&contentBytes.length<120){
boolean[][]codeOut=qrcodeHandler.calQrcode(contentBytes);
for(inti=0;i<codeOut.length;i++){
for(intj=0;j<codeOut.length;j++){
if(codeOut[j][i]){
gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
}
}
}
}else{
System.err.println("QRCodecontentbyteslength="
+contentBytes.length+"notin[0,120].");
return-1;
}
Imageimg=ImageIO.read(newFile(ccbPath));//實例化一個Image對象。
gs.drawImage(img,55,55,30,30,null);
gs.dispose();
bufImg.flush();

//生成二維碼QRCode圖片
FileimgFile=newFile(imgPath);
ImageIO.write(bufImg,"png",imgFile);

}catch(Exceptione){
e.printStackTrace();
return-100;
}
return0;
}

來自網友孤獨青鳥的博客

❸ 怎麼用java代碼把一個鏈接生成二維碼

參考代碼
import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class QRCodeEncoderTest
{
/** Creates a new instance of QRCodeEncoderTest */
public QRCodeEncoderTest()
{
}
public static void create_image(String sms_info)throws Exception{
try{
qrcode testQrcode =new qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);
String testString = sms_info;
byte[] d = testString.getBytes("gbk");
System.out.println(d.length);
//BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_INT_RGB);
BufferedImage bi = new BufferedImage(98, 98,

❹ 如何使用java,servlet創建二維碼

java需要Zebra Crossing(ZXing),你可以去他們網站下載,然後下面是一個例子程序

importjava.io.ByteArrayOutputStream;

importjava.io.File;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importnet.glxn.qrgen.QRCode;

importnet.glxn.qrgen.image.ImageType;

publicclass

Main {

publicstatic

void
main(String[] args) {

ByteArrayOutputStream
out = QRCode.from("Hello
World").to(ImageType.PNG).stream();

try{

FileOutputStream
fout = newFileOutputStream(newFile(

"C:QR_Code.JPG"));

fout.write(out.toByteArray());

fout.flush();

fout.close();

}catch(FileNotFoundException
e) {

//
Do Logging

}catch(IOException
e) {

//
Do Logging

}

}

}

閱讀全文

與javaqrcode相關的資料

熱點內容
jsjson工具 瀏覽:299
資料庫中如何備份一張表的數據 瀏覽:739
網路設備能用到什麼 瀏覽:64
暴風轉碼如何添加文件夾 瀏覽:515
延安整合網路營銷有哪些 瀏覽:74
查找word打開過的文件在哪裡 瀏覽:137
b樹java代碼 瀏覽:683
電腦文件存儲 瀏覽:657
蘭州中考徵集志願在哪個網站 瀏覽:215
cs文件上傳下載 瀏覽:244
拷貝文件到根目錄下重命名linux 瀏覽:603
api函數的頭文件 瀏覽:249
華為怎麼綁定迷你編程 瀏覽:215
機構怎麼申請少兒編程考級 瀏覽:495
崑山數控編程哪裡好學 瀏覽:459
jspcfor跳出 瀏覽:65
word怎麼插入羅馬數字i 瀏覽:315
哪個網站可以找到法人代表 瀏覽:106
蘋果5s日版a1453支持什麼網路 瀏覽:297
微信打開文件如何設置 瀏覽:218

友情鏈接