画像のダウンロード時にネストされた背景図と文字
6084 ワード
1.ネットワークファイルのダウンロード
2.画像合成に文字を追加
1、BatchNumberUtilsツール類
2、画像合成全コード
@GetMapping("downgd")
@ResponseBody
public Result downloadNet(HttpServletResponse response,String path) throws MalformedURLException {
//
Result result = new Result();
URL url = new URL(path);
try {
UUID uuid = UUID.randomUUID();
URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream();
OutputStream out = response.getOutputStream();
response.setHeader("content-disposition","
attachment;
filename=" +URLEncoder.encode(uuid+".jpg", "UTF-8"));
byte [] by = new byte[1024];
int i = 0;
while((i=in.read(by))!=-1)
{
out.write(by,0,i);//
}
//
//FileOutputStream fs =
// new FileOutputStream("e:/"+uuid+".jpg");
//byte[] buffer = new byte[1204];
//int length;
//while ((byteread = inStream.read(buffer)) != -1) {
// bytesum += byteread;
// fs.write(buffer, 0, byteread);
//}
in.close();
}catch (Exception e){
e.printStackTrace();
result.setCode(Result.Code.SYSTEMERROR);
}
return result;
}
2.画像合成に文字を追加
1、BatchNumberUtilsツール類
package com.cy.file_onload.qrCode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
public class BatchNumberUtils {
private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
private static final AtomicInteger atomicInteger = new AtomicInteger(1000000);
//
//@param no
// @return 、
public static synchronized String getOrderNoByUUID(String no) {
Integer uuidHashCode = UUID.randomUUID().toString().hashCode();
if (uuidHashCode < 0) {
uuidHashCode = uuidHashCode * (-1);
}
String date = simpleDateFormat.format(new Date());
return no + date + uuidHashCode;
}
//
//@param no
// @return
public static synchronized String getOrderNoByAtomic(String no) {
atomicInteger.getAndIncrement();
int i = atomicInteger.get();
String date = simpleDateFormat.format(new Date());
return no + date + i;
}
//
//@param name
// @return
public static synchronized String getFileNameByDate(String name) {
String date = dateFormat.format(new Date());
return name +"/"+ date;
}
}
2、画像合成全コード
@Controller
@RequestMapping("/test/")
public class ResumeTemplateServiceImpl {
private static String
ComposeFileNameManual = "compose_images";
// url , ,
// , upload_flowChart
private String resourceHandler="/upload_flowChart/";
// , @Value
// , E:/java/upload_flowChart
private String uploadImagesLocation = "E://";
// * @param templatePath
// @param seedPath
// @return
@RequestMapping("imgAdd")
public String composePic(String templatePath, String seedPath) {
templatePath = " ";
seedPath = " ";
try {
//
String picName = UUID.randomUUID().toString() + ".jpg";
//
String composeFileName = BatchNumberUtils.getFileNameByDate(ComposeFileNameManual);
//
File pathFile = new File(uploadImagesLocation + File.separator + composeFileName);
//
String path = uploadImagesLocation + File.separator + composeFileName + File.separator + picName;
//
String dataPath = resourceHandler.substring(0, resourceHandler.length() - 2) + composeFileName + "/" + picName;
if (seedPath == null) {
File file = new File(seedPath);
if (!file.isFile()) {
System.out.println(" ");
}
System.out.println(" ");
}
if (templatePath == null) {
File file = new File(templatePath);
if (!file.isFile()) {
System.out.println(" ");
}
System.out.println(" ");
}
if (!pathFile.exists()) {
pathFile.mkdirs();
}
//--------------------------------- -----------------------------
// File templateFlie = new File(templatePath);
BufferedImage bg = ImageIO.read(templateFlie);//
int height = bg.getHeight();//
int width = bg.getWidth(); //
BufferedImage qcCode = ImageIO.read(new File(seedPath)); // 300 * 300
BufferedImage img = new BufferedImage(184, 312, BufferedImage.TYPE_INT_RGB);//
Graphics g = img.getGraphics();//
// g.drawImage(bg.getScaledInstance(184, 312, Image.SCALE_DEFAULT), 0, 0, null); //
// /* g.drawImage(qcCode.getScaledInstance(width / 4, width / 4, Image.SCALE_DEFAULT),
width - (width / 4)-10, height - (width / 4)- 10, null); // */ // ,
g.drawImage(qcCode.getScaledInstance(180, 180, Image.SCALE_DEFAULT),
1, height/23, null); //
Font font = new Font(" ", Font.PLAIN, 15);//
g.setFont(font);
g.setColor(Color.BLACK);
//
String lastChar = " ".substring(" ".length() - 1);
//
String newUserName = " ".substring(0, 1) + "**" + lastChar + " ";
//
g.drawString(newUserName, 2, 270);
//
g.dispose();
ImageIO.write(img, "jpg", new File(path));
System.out.println(" , :" + path);
File file = new File(path);
System.out.println(file);
//
return dataPath;
} catch (Exception e) {
return e.getMessage();
//throw new CustomException(" ", 400);
}
}
}