import javax. imageio. ImageIO;
import java. awt. *;
import java. awt. image. BufferedImage;
import java. io. File;
import java. io. IOException;
import java. net. URL;
public class ImageMerging {
private Font font = new Font ( "微软雅黑" , Font. PLAIN, 46 ) ;
private Graphics2D g = null;
private int fontsize = 0 ;
private int x = 0 ;
private int y = 0 ;
public BufferedImage loadImageLocal ( String imgName) {
try {
return ImageIO. read ( new File ( imgName) ) ;
} catch ( IOException e) {
System. out. println ( e. getMessage ( ) ) ;
}
return null;
}
public BufferedImage loadImageUrl ( String imgName) {
try {
URL url = new URL ( imgName) ;
return ImageIO. read ( url) ;
} catch ( IOException e) {
System. out. println ( e. getMessage ( ) ) ;
}
return null;
}
public void writeImageLocal ( String newImage, BufferedImage img) {
if ( newImage != null && img != null) {
try {
File outputfile = new File ( newImage) ;
ImageIO. write ( img, "jpg" , outputfile) ;
} catch ( IOException e) {
System. out. println ( e. getMessage ( ) ) ;
}
}
}
public void setFont ( String fontStyle, int fontSize) {
this . fontsize = fontSize;
this . font = new Font ( fontStyle, Font. PLAIN, fontSize) ;
}
public BufferedImage modifyImage ( BufferedImage img, Object content, int x, int y) {
try {
int w = img. getWidth ( ) ;
int h = img. getHeight ( ) ;
g = img. createGraphics ( ) ;
g. setBackground ( new Color ( 255 , 255 , 255 , 0 ) ) ;
g. setColor ( Color. WHITE) ;
g. setFont ( new Font ( "微软雅黑" , Font. PLAIN, 46 ) ) ;
if ( this . font != null)
g. setFont ( this . font) ;
if ( x >= h || y >= w) {
this . x = h - this . fontsize + 2 ;
this . y = w;
} else {
this . x = x;
this . y = y;
}
if ( content != null) {
g. drawString ( content. toString ( ) , this . x, this . y) ;
}
g. dispose ( ) ;
} catch ( Exception e) {
System. out. println ( e. getMessage ( ) ) ;
}
return img;
}
public BufferedImage modifyImage ( BufferedImage img, Object[ ] contentArr, int x, int y,
boolean xory) {
try {
int w = img. getWidth ( ) ;
int h = img. getHeight ( ) ;
g = img. createGraphics ( ) ;
g. setBackground ( Color. WHITE) ;
g. setColor ( Color. RED) ;
if ( this . font != null)
g. setFont ( this . font) ;
if ( x >= h || y >= w) {
this . x = h - this . fontsize + 2 ;
this . y = w;
} else {
this . x = x;
this . y = y;
}
if ( contentArr != null) {
int arrlen = contentArr. length;
if ( xory) {
for ( int i = 0 ; i < arrlen; i++ ) {
g. drawString ( contentArr[ i] . toString ( ) , this . x, this . y) ;
this . x += contentArr[ i] . toString ( ) . length ( ) * this . fontsize / 2 + 5 ;
}
} else {
for ( int i = 0 ; i < arrlen; i++ ) {
g. drawString ( contentArr[ i] . toString ( ) , this . x, this . y) ;
this . y += this . fontsize + 2 ;
}
}
}
g. dispose ( ) ;
} catch ( Exception e) {
System. out. println ( e. getMessage ( ) ) ;
}
return img;
}
public BufferedImage modifyImageYe ( BufferedImage img) {
try {
int w = img. getWidth ( ) ;
int h = img. getHeight ( ) ;
g = img. createGraphics ( ) ;
g. setBackground ( Color. WHITE) ;
g. setColor ( Color. blue) ;
if ( this . font != null)
g. setFont ( this . font) ;
g. dispose ( ) ;
} catch ( Exception e) {
System. out. println ( e. getMessage ( ) ) ;
}
return img;
}
public BufferedImage modifyImagetogeter ( BufferedImage b, BufferedImage d, int width, int height) {
try {
int w = b. getWidth ( ) ;
int h = b. getHeight ( ) ;
g = d. createGraphics ( ) ;
g. drawImage ( b, width, height, w, h, null) ;
g. dispose ( ) ;
} catch ( Exception e) {
System. out. println ( e. getMessage ( ) ) ;
}
return d;
}
public static void main ( String[ ] args) {
ImageMerging tt = new ImageMerging ( ) ;
BufferedImage b = tt. loadImageLocal ( "E:\\image\\33.png" ) ;
BufferedImage d = tt. loadImageLocal ( "E:\\image\\44.png" ) ;
BufferedImage aaaaaaaa = tt. modifyImage ( b, "勇衣小吃店" , 578 , 999 ) ;
tt. writeImageLocal ( "E:\\image\\cc.jpg" , tt. modifyImagetogeter ( d, aaaaaaaa, 342 , 218 ) ) ;
System. out. println ( "success" ) ;
}
}
如果想要复用一张背景图片,并且想要每次给背景图片上面添加不同内容,那就需要将复用的背景图片进行拷贝,然后对副本进行拷贝,可以减少IO操作,提高效率。
public BufferedImage copyImage ( BufferedImage source) {
BufferedImage copyBufferedImage = new BufferedImage ( source. getWidth ( ) , source. getHeight ( ) , source. getType ( ) ) ;
Graphics g = copyBufferedImage. getGraphics ( ) ;
g. drawImage ( source, 0 , 0 , null) ;
g. dispose ( ) ;
return copyBufferedImage;
}
public BufferedImage qRCodeGeneration ( ImageMerging pic, String unitName, BufferedImage qRCode, BufferedImage backgroundImage, FontMetrics fontMetrics) {
int strWidth = fontMetrics. stringWidth ( unitName) ;
BufferedImage copyBufferedImage = copyImage ( backgroundImage) ;
BufferedImage backgroundAndName = pic. modifyImage ( copyBufferedImage, unitName, ( 1400 - strWidth) / 2 , 1050 ) ;
BufferedImage bufferedImage = pic. modifyImagetogeter ( qRCode, backgroundAndName, 342 , 218 ) ;
return bufferedImage;
}
实现批量下载,和照片批量生成
public void generateQRCode ( @RequestParam ( required = false ) Long unitId, HttpServletResponse response) throws Exception {
long start = System. currentTimeMillis ( ) ;
List< DrugUnitInfo> drugUnitList = new ArrayList < > ( ) ;
if ( unitId == null) {
drugUnitList = drugUnitInfoService. list ( ) ;
} else {
DrugUnitInfo drugUnitInfo = drugUnitInfoService. getById ( unitId) ;
drugUnitList. add ( drugUnitInfo) ;
}
String downloadFilename = "药品二维码" ;
downloadFilename = URLEncoder. encode ( downloadFilename, "UTF-8" ) ;
ZipOutputStream zos = new ZipOutputStream ( response. getOutputStream ( ) ) ;
response. setContentType ( "application/octet-stream" ) ;
response. setHeader ( "Content-disposition" , "attachment; filename=" + downloadFilename + ".zip" ) ;
AtomicReference< BufferedImage> encode = new AtomicReference < > ( ) ;
ImageMerging pic = new ImageMerging ( ) ;
FontMetrics metrics = new JLabel ( ) . getFontMetrics ( new Font ( "微软雅黑" , Font. PLAIN, 46 ) ) ;
BufferedImage backgroundImage = pic. loadImageLocal ( "E:\\image\\33.png" ) ;
drugUnitList. forEach ( i - > {
if ( ! StringUtils. isEmpty ( i. getUnitNo ( ) ) ) {
try {
encode. set ( QRCodeUtil. encode ( host + ":" + port + urlPrefix + i. getId ( ) , null, true ) ) ;
BufferedImage bufferedImage = pic. qRCodeGeneration ( pic, i. getUnitName ( ) , encode. get ( ) , backgroundImage, metrics) ;
zos. putNextEntry ( new ZipEntry ( i. getUnitName ( ) + ".png" ) ) ;
ImageIO. write ( bufferedImage, "png" , zos) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
} ) ;
zos. flush ( ) ;
zos. close ( ) ;
long end = System. currentTimeMillis ( ) ;
System. out. println ( "执行完时间:" + ( end - start) ) ;
}
完整版ImageMerging
package com. swkj. smart. market. regulation. sysmanage. utils;
import javax. imageio. ImageIO;
import java. awt. *;
import java. awt. image. BufferedImage;
import java. io. File;
import java. io. IOException;
import java. net. URL;
public class ImageMerging {
private Font font = new Font ( "微软雅黑" , Font. PLAIN, 46 ) ;
private Graphics2D g = null;
private int fontsize = 0 ;
private int x = 0 ;
private int y = 0 ;
public BufferedImage loadImageLocal ( String imgName) {
try {
return ImageIO. read ( new File ( imgName) ) ;
} catch ( IOException e) {
System. out. println ( e. getMessage ( ) ) ;
}
return null;
}
public BufferedImage loadImageUrl ( String imgName) {
try {
URL url = new URL ( imgName) ;
return ImageIO. read ( url) ;
} catch ( IOException e) {
System. out. println ( e. getMessage ( ) ) ;
}
return null;
}
public void writeImageLocal ( String newImage, BufferedImage img) {
if ( newImage != null && img != null) {
try {
File outputfile = new File ( newImage) ;
ImageIO. write ( img, "jpg" , outputfile) ;
} catch ( IOException e) {
System. out. println ( e. getMessage ( ) ) ;
}
}
}
public void setFont ( String fontStyle, int fontSize) {
this . fontsize = fontSize;
this . font = new Font ( fontStyle, Font. PLAIN, fontSize) ;
}
public BufferedImage modifyImage ( BufferedImage img, Object content, int x, int y) {
try {
int w = img. getWidth ( ) ;
int h = img. getHeight ( ) ;
g = img. createGraphics ( ) ;
g. setBackground ( new Color ( 255 , 255 , 255 , 0 ) ) ;
g. setColor ( Color. WHITE) ;
g. setFont ( new Font ( "微软雅黑" , Font. PLAIN, 46 ) ) ;
if ( this . font != null)
g. setFont ( this . font) ;
if ( x >= h || y >= w) {
this . x = h - this . fontsize + 2 ;
this . y = w;
} else {
this . x = x;
this . y = y;
}
if ( content != null) {
g. drawString ( content. toString ( ) , this . x, this . y) ;
}
g. dispose ( ) ;
} catch ( Exception e) {
System. out. println ( e. getMessage ( ) ) ;
}
return img;
}
public BufferedImage modifyImage ( BufferedImage img, Object[ ] contentArr, int x, int y,
boolean xory) {
try {
int w = img. getWidth ( ) ;
int h = img. getHeight ( ) ;
g = img. createGraphics ( ) ;
g. setBackground ( Color. WHITE) ;
g. setColor ( Color. RED) ;
if ( this . font != null)
g. setFont ( this . font) ;
if ( x >= h || y >= w) {
this . x = h - this . fontsize + 2 ;
this . y = w;
} else {
this . x = x;
this . y = y;
}
if ( contentArr != null) {
int arrlen = contentArr. length;
if ( xory) {
for ( int i = 0 ; i < arrlen; i++ ) {
g. drawString ( contentArr[ i] . toString ( ) , this . x, this . y) ;
this . x += contentArr[ i] . toString ( ) . length ( ) * this . fontsize / 2 + 5 ;
}
} else {
for ( int i = 0 ; i < arrlen; i++ ) {
g. drawString ( contentArr[ i] . toString ( ) , this . x, this . y) ;
this . y += this . fontsize + 2 ;
}
}
}
g. dispose ( ) ;
} catch ( Exception e) {
System. out. println ( e. getMessage ( ) ) ;
}
return img;
}
public BufferedImage modifyImageYe ( BufferedImage img) {
try {
int w = img. getWidth ( ) ;
int h = img. getHeight ( ) ;
g = img. createGraphics ( ) ;
g. setBackground ( Color. WHITE) ;
g. setColor ( Color. blue) ;
if ( this . font != null)
g. setFont ( this . font) ;
g. dispose ( ) ;
} catch ( Exception e) {
System. out. println ( e. getMessage ( ) ) ;
}
return img;
}
public BufferedImage modifyImagetogeter ( BufferedImage b, BufferedImage d, int width, int height) {
try {
int w = b. getWidth ( ) ;
int h = b. getHeight ( ) ;
g = d. createGraphics ( ) ;
g. drawImage ( b, width, height, w, h, null) ;
g. dispose ( ) ;
} catch ( Exception e) {
System. out. println ( e. getMessage ( ) ) ;
}
return d;
}
public BufferedImage copyImage ( BufferedImage source) {
BufferedImage copyBufferedImage = new BufferedImage ( source. getWidth ( ) , source. getHeight ( ) , source. getType ( ) ) ;
Graphics g = copyBufferedImage. getGraphics ( ) ;
g. drawImage ( source, 0 , 0 , null) ;
g. dispose ( ) ;
return copyBufferedImage;
}
public BufferedImage qRCodeGeneration ( ImageMerging pic, String unitName, BufferedImage qRCode, BufferedImage backgroundImage, FontMetrics fontMetrics) {
int strWidth = fontMetrics. stringWidth ( unitName) ;
BufferedImage copyBufferedImage = copyImage ( backgroundImage) ;
BufferedImage backgroundAndName = pic. modifyImage ( copyBufferedImage, unitName, ( 1400 - strWidth) / 2 , 1050 ) ;
BufferedImage bufferedImage = pic. modifyImagetogeter ( qRCode, backgroundAndName, 342 , 218 ) ;
return bufferedImage;
}
public static void main ( String[ ] args) {
ImageMerging tt = new ImageMerging ( ) ;
BufferedImage b = tt. loadImageLocal ( "E:\\image\\33.png" ) ;
BufferedImage d = tt. loadImageLocal ( "E:\\image\\44.png" ) ;
BufferedImage aaaaaaaa = tt. modifyImage ( b, "勇衣小吃店" , 578 , 999 ) ;
tt. writeImageLocal ( "E:\\image\\cc.jpg" , tt. modifyImagetogeter ( d, aaaaaaaa, 342 , 218 ) ) ;
System. out. println ( "success" ) ;
}
}