package com.doumee.core.utils; import java.awt.*; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; //import com.sun.image.codec.jpeg.JPEGCodec; //import com.sun.image.codec.jpeg.JPEGImageEncoder; import javax.imageio.ImageIO; public class ImageDesignerUtil { private static BufferedImage image; private static void createImage(String fileLocation,BufferedImage image) { try { // FileOutputStream fos = new FileOutputStream(fileLocation); // BufferedOutputStream bos = new BufferedOutputStream(fos); // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); // encoder.encode(image); // bos.close(); String formatName = fileLocation.substring(fileLocation.lastIndexOf(".") + 1); ImageIO.write(image, formatName , new File(fileLocation) ); } catch (Exception e) { e.printStackTrace(); } } public static boolean graphicsGenerationIs(String bottom, InputStream imgurl, String savePath) { try { int imageWidth = 320;// 图片的宽度 int imageHeight = 370;// 图片的高度 BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics(); graphics.setColor(Color.black); graphics.fillRect(0, 0, imageWidth, imageHeight); // 改成这样: BufferedImage bimg = null; try { bimg = javax.imageio.ImageIO.read(imgurl); } catch (Exception e) { } if (bimg != null) graphics.drawImage(bimg, 15, 15, null); // ------------------------背景图画结束-------------- graphics.setColor(Color.WHITE); graphics.setFont(new Font("微软雅黑", Font.PLAIN, 30)); graphics.drawString(bottom, 90, 345); // --------------------昵称画结束------------------ graphics.dispose(); createImage(savePath,image); return new File(savePath).isFile(); }catch (Exception e){ return false; } } public static String graphicsGeneration(String name, String imgurl, String header, String qrCode,String savePath) { int imageWidth = 640;// 图片的宽度 int imageHeight = 1020;// 图片的高度 image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, imageWidth, imageHeight); // 改成这样: BufferedImage bimg = null; try { bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl)); } catch (Exception e) { } if (bimg != null) graphics.drawImage(bimg, 0, 0, null); // ------------------------背景图画结束-------------- // graphics.fillRect(0, 0, headWidth, headHeight); // 改成这样: bimg = null; try { bimg = javax.imageio.ImageIO.read(new java.io.File(header)); } catch (Exception e) { } if (bimg != null) graphics.drawImage(bimg, 26, 34, 90, 90, null); // --------------头像画结束--------------- graphics.setColor(Color.BLACK); graphics.setFont(new Font("微软雅黑", Font.PLAIN, 30)); graphics.drawString(name, 134, 70); // --------------------昵称画结束------------------ graphics.setColor(Color.WHITE); // graphics.fillRect(0, 0, codeWidth, codeHeight); // 改成这样: bimg = null; try { bimg = javax.imageio.ImageIO.read(new java.io.File(qrCode)); } catch (Exception e) { } if (bimg != null) graphics.drawImage(bimg, 140, 547, 360, 360, null); // --------------二维码画结束--------------- graphics.dispose(); createImage(savePath,image); return savePath; } public static void main(String[] args) { ImageDesignerUtil cg = new ImageDesignerUtil(); try { graphicsGeneration("我是新世界之神", "bg.png", "D:\\test\\head.png", "D:\\test\\code.png","D:\\test\\test0.png"); } catch (Exception e) { e.printStackTrace(); } } }