代码直接可以用
public static void main(String[] args) {
test();
}
private static void test() {
try {
BufferedImage img = new BufferedImage(533, 800, BufferedImage.TYPE_INT_RGB);//创建图片
BufferedImage bg = ImageIO.read(new URL("http://wx.ocheng.me/storage//M00/00/16/CgICAlsQ27CAdNuEAACmxy2K9iE437.jpg"));//背景图片
BufferedImage logo = ImageIO.read(new URL("http://wx.ocheng.me/storage//M00/00/12/CgICAlsMtNqAeC8yAAAnKdk5T_890.jpeg"));//读取互联网图片
BufferedImage er = ImageIO.read(new URL("http://wx.ocheng.me/storage//M00/00/12/CgICAlsMtV2AA7lDAAB_xIM4l9E208.jpg"));//插入的图片
Graphics g = img.getGraphics();//开启画图
g.drawImage(bg.getScaledInstance(533, 800, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图
g.drawImage(logo.getScaledInstance(75, 65, Image.SCALE_DEFAULT), 410, 705, null); // 绘制缩小后的图
g.drawImage(er.getScaledInstance(160, 215, Image.SCALE_DEFAULT), 185, 266, null); // 绘制缩小后的图
g.setColor(Color.black);
g.setFont(new Font("宋体", Font.BOLD, 18));
g.setColor(Color.decode("#d2a67e"));
g.drawString("张勇", 270, 522);//绘制文字
g.drawString("57", 273, 543);
g.dispose();
ByteArrayOutputStream s = new ByteArrayOutputStream();
try {
// ImageIO.write(img, "jpg", s);
ImageIO.write(img, "jpg", new File("D:/aaa/sdf.jpg"));
// byte[] b = s.toByteArray();
// StorageService storageService = new StorageServiceImpl();
// StorageEntity jpg = storageService.store(b, "jpg");
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//可能会出现在图片上写字编码不对 因为Linux 系统的没有字体 需要在Linux系统上上传Windows系统是字体
public static void main(String[] args) {
test();
}
private static void test() {
try {
BufferedImage img = new BufferedImage(533, 800, BufferedImage.TYPE_INT_RGB);//创建图片
BufferedImage bg = ImageIO.read(new URL("http://wx.ocheng.me/storage//M00/00/16/CgICAlsQ27CAdNuEAACmxy2K9iE437.jpg"));//背景图片
// BufferedImage logo = ImageIO.read(new URL("http://wx.ocheng.me/storage//M00/00/12/CgICAlsMtNqAeC8yAAAnKdk5T_890.jpeg"));//读取互联网图片
BufferedImage er = ImageIO.read(new URL("http://wx.ocheng.me/storage//M00/00/17/CgICAlsUyyeAfDpFABk_mNPCgoc648.jpg"));//插入的图片
download( "http://wx.ocheng.me/storage//M00/00/17/CgICAlsUyyeAfDpFABk_mNPCgoc648.jpg" ,"image.jpg");
File imagefile=new File("image.jpg");
Metadata metadata = ImageMetadataReader.readMetadata(imagefile);
Map<String, Object> map = FileUtil.printExif(imagefile, metadata);
int angle = FileUtil.getAngle(map);
BufferedImage bf = FileUtil.getBufferedImg(er, null, er.getWidth(), er.getHeight(), angle);
Graphics g = img.getGraphics();//开启画图
g.drawImage(bg.getScaledInstance(533, 800, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图
// g.drawImage(logo.getScaledInstance(75, 65, Image.SCALE_DEFAULT), 410, 705, null); // 绘制缩小后的图
g.drawImage(bf.getScaledInstance(160, 215, Image.SCALE_DEFAULT), 185, 266, null); // 绘制缩小后的图
g.setColor(Color.black);
g.setFont(new Font("宋体", Font.BOLD, 18));
g.setColor(Color.decode("#d2a67e"));
g.drawString("张勇", 270, 522);//绘制文字
g.drawString("57", 273, 543);
g.dispose();
ByteArrayOutputStream s = new ByteArrayOutputStream();
try {
// ImageIO.write(img, "jpg", s);
ImageIO.write(img, "jpg", new File("D:/aaa/ss/sdf.jpg"));
// byte[] b = s.toByteArray();
// StorageService storageService = new StorageServiceImpl();
// StorageEntity jpg = storageService.store(b, "jpg");
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public MkResponse upload(@RequestParam("file") MultipartFile file) {
MkResponse response = new MkResponse(MessageCode.SUCCESS);
if (file == null) {
return new MkResponse(MessageCode.INVALID_PARAMETER);
}
String[] array = org.springframework.util.StringUtils.split(file.getOriginalFilename(), ".");
String ext = "";
if (array != null && array.length > 1) {
//文件没有后缀名
ext = array[array.length - 1];
}
try {
File imagefile = new File("image.jpg");
FileUtils.copyInputStreamToFile((InputStream) file, imagefile);
Metadata metadata = ImageMetadataReader.readMetadata(imagefile);
Map<String, Object> map = FileUtil.printExif(imagefile, metadata);
int angle = FileUtil.getAngle(map);
BufferedImage bg = ImageIO.read(new File(file.getName()));
BufferedImage bf = FileUtil.getBufferedImg(bg, null, bg.getWidth(), bg.getHeight(), angle);
ByteArrayOutputStream sd = new ByteArrayOutputStream();
ImageIO.write(bf, "jpg", sd);
byte[] b = sd.toByteArray();
StorageService storageService = new StorageServiceImpl();
//StorageEntity jpg = storageService.store(b, "jpg");
StorageEntity storageEntity = storageService.store(b, ext);
response.setData(storageEntity.getUrl());
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public static void main(String[] args) {
test();
}
private static void test() {
try {
BufferedImage img = new BufferedImage(533, 800, BufferedImage.TYPE_INT_RGB);//创建图片
BufferedImage bg = ImageIO.read(new URL("http://wx.ocheng.me/storage//M00/00/16/CgICAlsQ7VKAD4D6AAC-5L3S08o650.jpg"));//背景图片
// BufferedImage logo = ImageIO.read(new URL("http://wx.ocheng.me/storage//M00/00/12/CgICAlsMtNqAeC8yAAAnKdk5T_890.jpeg"));//读取互联网图片
BufferedImage er = ImageIO.read(new URL("http://bosch.weicats.com/ocpic/%E7%8E%8B%E5%A7%BF%E8%AF%BA.jpg"));//插入的图片
// download( "http://bosch.weicats.com/ocpic/%E7%8E%8B%E5%8D%9A%E6%AF%85.jpg" ,"image.jpg");
// File imagefile=new File("image.jpg");
// Metadata metadata = ImageMetadataReader.readMetadata(imagefile);
// Map<String, Object> map = FileUtil.printExif(imagefile, metadata);
// int angle = FileUtil.getAngle(map);
// BufferedImage bf = FileUtil.getBufferedImg(er, null, er.getWidth(), er.getHeight(), angle);
Graphics g = img.getGraphics();//开启画图
g.drawImage(bg.getScaledInstance(533, 800, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图
// g.drawImage(logo.getScaledInstance(75, 65, Image.SCALE_DEFAULT), 410, 705, null); // 绘制缩小后的图
g.drawImage(er.getScaledInstance(160, 215, Image.SCALE_DEFAULT), 185, 266, null); // 绘制缩小后的图
g.setColor(Color.black);
g.setFont(new Font("宋体", Font.BOLD, 18));
g.setColor(Color.decode("#d2a67e"));
g.drawString("王姿诺", 270, 522);//绘制文字
g.drawString("100", 273, 543);
g.dispose();
ByteArrayOutputStream s = new ByteArrayOutputStream();
try {
// ImageIO.write(img, "jpg", s);
ImageIO.write(img, "jpg", new File("D:/aaa/wzn.jpg"));
// byte[] b = s.toByteArray();
// StorageService storageService = new StorageServiceImpl();
// StorageEntity jpg = storageService.store(b, "jpg");
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}