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

熱點內容
西門子jmp指令在編程軟體哪裡 瀏覽:335
win10加密共享文件夾不見了 瀏覽:412
平板電腦無法打開文件 瀏覽:718
win10如何強制重命名文件 瀏覽:204
java集合隨機打亂順序 瀏覽:805
qq怎麼解綁app 瀏覽:995
蘋果設備優盤考文件 瀏覽:662
java項目部署與測試 瀏覽:728
個人微信號和公眾平台 瀏覽:988
微信顯示網路不可用 瀏覽:478
現實編程怎麼敲鍵盤 瀏覽:950
怎麼做招標文件 瀏覽:543
黑蘋果macminidiy 瀏覽:105
系統暫時刪除應用程序 瀏覽:400
怎麼更改qq郵箱的賬號和密碼 瀏覽:947
電視劇周邊是什麼網路用語 瀏覽:130
網路信號怎麼來的 瀏覽:802
win10文件默認顏色 瀏覽:713
安卓的榮耀戰區 瀏覽:621
使excel文件損壞的原因 瀏覽:746

友情鏈接