美文网首页
Java 在图片上写文字和图片

Java 在图片上写文字和图片

作者: 代瑶 | 来源:发表于2021-01-04 10:58 被阅读0次
    写文字之前.png
    写文字之后.png

    import com.sun.image.codec.jpeg.ImageFormatException;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageDecoder;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;

    import javax.imageio.ImageIO;
    import javax.swing.;
    import java.awt.
    ;
    import java.awt.font.FontRenderContext;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.io.*;

    public class MainIn {

    static String picturePath;
    static String pictureInName = "/bg.jpeg";
    static String pictureOutName = "/bg_out.jpeg";
    
    
    public static void main(String[] args) {
        String _dir = System.getProperty("user.dir");
        picturePath = _dir + File.separator + "src" + File.separator;
    

    // exportImg1();
    drawTextToPicture("我好,中国", picturePath + "/head_img.png");
    }

    ///在图片上画文字
    public static void drawTextToPicture(String username, String headImg) {
        try {
            //1.jpg是你的 主图片的路径
            InputStream is = new FileInputStream(picturePath + pictureInName);
    
    
            //通过JPEG图象流创建JPEG数据流解码器
            JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
            //解码当前JPEG数据流,返回BufferedImage对象
            BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
            //得到画笔对象
            Graphics g = buffImg.getGraphics();
    
            //创建你要附加的图象。
            //小图片的路径
            ImageIcon imgIcon = new ImageIcon(headImg);
    
            //得到Image对象。
            Image img = imgIcon.getImage();
    
            //将小图片绘到大图片上。
            //5,300 .表示你的小图片在大图片上的位置。
            g.drawImage(img, 400, 15, null);
    
            //设置颜色。
            g.setColor(Color.BLACK);
    
    
            //最后一个参数用来设置字体的大小
            Font f = new Font("宋体", Font.PLAIN, 25);
            Color mycolor = Color.red;//new Color(0, 0, 255);
            g.setColor(mycolor);
            g.setFont(f);
    
            //10,20 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
            g.drawString(username, 100, 135);
    
            g.dispose();
            OutputStream os;
    
            os = new FileOutputStream(picturePath + pictureOutName);
            //创键编码器,用于编码内存中的图象数据。
            JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
            en.encode(buffImg);
    
            is.close();
            os.close();
        } catch (ImageFormatException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    ///创建一个文本画布
    public static void createTextCanvas() {
        int width = 100;
        int height = 100;
        String s = "你好";
    
        File file = new File(picturePath + pictureInName);
        Font font = new Font("Serif", Font.BOLD, 10);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, width, height);
        g2.setPaint(Color.RED);
    
        FontRenderContext context = g2.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(s, context);
        double x = (width - bounds.getWidth()) / 2;
        double y = (height - bounds.getHeight()) / 2;
        double ascent = -bounds.getY();
        double baseY = y + ascent;
    
        g2.drawString(s, (int) x, (int) baseY);
    
        try {
            ImageIO.write(bi, "jpg", file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    }

    
    2021年01月15日13:29:09更新
     文字增加抗锯齿, 文本x  y 坐标校准
    ~~~
    
    
    
        ///在图片上画文字
        public static void drawTextToPicture(List<DetailCoordinate> _detailList) {
            try {
    
                BufferedImage _mainImage = getImage(picturePath + pictureInName);
                SunGraphics2D g = (SunGraphics2D) _mainImage.getGraphics();
    
                g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    
                OutputStream os = new FileOutputStream(picturePath + pictureOutName);
                for (DetailCoordinate itemCoordinate : _detailList) {
                    if (itemCoordinate.type == 0) {
                        //最后一个参数用来设置字体的大小
                        Font f = new Font(Font.SERIF, Font.PLAIN, itemCoordinate.size);
                        Color myColor = fromStrToARGB(itemCoordinate.getColor());//new Color(0, 0, 255);
                        g.setColor(myColor);
                        g.setFont(f);
    
                        FontMetrics fm = g.getFontMetrics();
                        g.drawString(itemCoordinate.txt, itemCoordinate.x, fm.getMaxAscent() + itemCoordinate.y);
                    } else if (itemCoordinate.type == 1) {
                        //创建你要附加的图象。
                        //小图片的路径
                        ImageIcon imgIcon = new ImageIcon(itemCoordinate.txt);
    
                        //得到Image对象。
                        Image img = imgIcon.getImage();
    
                        //将小图片绘到大图片上。
                        //5,300 .表示你的小图片在大图片上的位置。
                        g.drawImage(img, itemCoordinate.x, itemCoordinate.y, null);
    
                        //设置颜色。
                        g.setColor(Color.BLACK);
                    }
                }
                g.dispose();
                _mainImage.flush();
    
                ImageIO.write(_mainImage, "png", os);
            } catch (ImageFormatException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    

    相关文章

      网友评论

          本文标题:Java 在图片上写文字和图片

          本文链接:https://www.haomeiwen.com/subject/hjnmoktx.html