導航:首頁 > 編程語言 > 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相關的資料

熱點內容
建行簡訊通知費用在app怎麼交 瀏覽:625
修改git密碼設置 瀏覽:606
自學編程可以看什麼書 瀏覽:151
有哪些少兒編程技術 瀏覽:204
extjs2實例 瀏覽:249
u盤直接拔出然後不能讀取文件 瀏覽:372
supcfgtxt是什麼文件 瀏覽:890
編程里cn是什麼意思 瀏覽:823
jsp如何獲取文本框的值 瀏覽:126
文件管理服務啟動時顯示找不到 瀏覽:920
怎樣刪除唱吧的錄音文件在哪裡 瀏覽:277
市場數據調研費用需要多少 瀏覽:575
150的號跑人環升級 瀏覽:17
鍛造780後怎麼升級 瀏覽:310
優盤里的視頻文件被殺毒軟體清理 瀏覽:29
學習編程需要什麼樣的筆記本電腦 瀏覽:322
隱藏的app如何顯示華為 瀏覽:141
榮耀v8系統版本b160 瀏覽:341
管理員怎麼刪除群文件 瀏覽:10
qt找不到其他文件夾的頭文件 瀏覽:183

友情鏈接