日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA生成的二维码以及给二维码添加背景图片

發(fā)布時間:2024/1/18 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA生成的二维码以及给二维码添加背景图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JAVA生成的二維碼以及給二維碼添加背景圖片**

1.頁面只需一行代碼即可(用ajax請求得不到響應,也可以用表單提交)

window.location.href = “${ctx}/qrcode/getQrcodes?id=”+id

2.后臺代碼

public void getQrcode(String id,HttpServletResponse
httpServletResponse){
InputStream imagein = null;
try {
String[] idStr=ids.split(",");
httpServletResponse.setContentType(“application/zip”);
httpServletResponse.addHeader(“Content-Disposition”,
“attachment;filename=”
+ URLEncoder.encode(“二維碼.zip”, “utf-8”));
OutputStream outputStream = httpServletResponse.getOutputStream(); ZipOutputStream
zipOutputStream = new ZipOutputStream(outputStream);
String text = “”; // 二維碼內(nèi)容PPNkkUNC4DNCQuDuOv
//String pressText = “商戶編號:”+merchant.getMerNo();//向享圖片中添加文字
int width = 330; // 二維碼圖片寬度
int height =330; // 二維碼圖片高度
String format = “png”;// 二維碼的圖片格式
//int fontStyle = 0; //字體風格
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”); // 內(nèi)容所使用字符集編碼
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, width, height,
hints);
//生成的二維碼
BufferedImage buffImg = MatrixToImageWriter.toBufferedImage(bitMatrix);
/Graphics gs = buffImg.createGraphics();
//設置畫筆的顏色
gs.setColor(Color.black);
//設置字體
Font font = new Font(“宋體”, fontStyle, 12);
FontMetrics metrics = gs.getFontMetrics(font);
//文字在圖片中的坐標 這里設置在中間
int startX = (width - metrics.stringWidth(pressText))/2 ;
int startY = height-300;
gs.setFont(font);
gs.drawString(pressText, startX, startY);/
//背景圖片的路徑
File logoFile = new File(QrcodeService.class.getClassLoader().getResource(“backgroundPicture.png”).getFile());
imagein = new FileInputStream(logoFile.toString());
//讀出背景圖片
BufferedImage image = ImageIO.read(imagein);
Graphics g = image.getGraphics();
//將二維碼畫到背景圖片中
g.drawImage(buffImg, 85, 100,buffImg.getWidth(), buffImg.getHeight(), null);
// 生成二維碼
ZipEntry entry=null;
entry = new ZipEntry(“圖片名字”+".png");
zipOutputStream.putNextEntry(entry);
ImageIO.write(image, format, zipOutputStream);
zipOutputStream.flush();
zipOutputStream.close();
outputStream.flush();
outputStream.close();
System.out.println(“完成二維碼生成”);
} catch(Exception e) {
e.printStackTrace();
throw new RuntimeException(“打包二維碼異常”);
}
}

3.pom文件

4.本人親測可用,不明白的請留言。

總結(jié)

以上是生活随笔為你收集整理的JAVA生成的二维码以及给二维码添加背景图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。