欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)在一張大圖片上添加小圖及文字

 更新時(shí)間:2021年11月12日 11:17:19   作者:Mr_LGZ  
這篇文章主要介紹了java實(shí)現(xiàn)在一張大圖上添加小圖及文字,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在一張大圖上添加小圖及文字

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.ParseException;
public class ImgUtil {
    public static void main(String[] args) throws ParseException {
        String bigImg = "C:\\Users\\langz\\Desktop\\ffff\\big.jpg";
        String smallImg = "C:\\Users\\langz\\Desktop\\ffff\\small.jpg";
        String content = "好久不見,你還好嗎";
        String outPath = "C:\\Users\\langz\\Desktop\\ffff\\" + System.currentTimeMillis() + ".jpg";
        try {
            bigImgAddSmallImgAndText(bigImg, smallImg, 500, 500, null, 200, 200, outPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /***
     * 在一張大圖張?zhí)砑有D和文字
     * @param bigImgPath 大圖的路徑
     * @param smallImgPath 小圖的路徑
     * @param sx    小圖在大圖上x抽位置
     * @param sy    小圖在大圖上y抽位置
     * @param content   文字內(nèi)容
     * @param cx    文字在大圖上y抽位置
     * @param cy    文字在大圖上y抽位置
     * @param outPathWithFileName 結(jié)果輸出路徑
     */
    public static void bigImgAddSmallImgAndText(String bigImgPath
            , String smallImgPath, int sx, int sy
            , String content, int cx, int cy
            , String outPathWithFileName) throws IOException {
        //主圖片的路徑
        InputStream is = new FileInputStream(bigImgPath);
        //通過(guò)JPEG圖象流創(chuàng)建JPEG數(shù)據(jù)流解碼器
        JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
        //解碼當(dāng)前JPEG數(shù)據(jù)流,返回BufferedImage對(duì)象
        BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
        //得到畫筆對(duì)象
        Graphics g = buffImg.getGraphics();
        //小圖片的路徑
        ImageIcon imgIcon = new ImageIcon(smallImgPath);
        //得到Image對(duì)象。
        Image img = imgIcon.getImage();
        //將小圖片繪到大圖片上,5,300 .表示你的小圖片在大圖片上的位置。
        g.drawImage(img, sx, sy, null);
        //設(shè)置顏色。
        g.setColor(Color.WHITE);
        //最后一個(gè)參數(shù)用來(lái)設(shè)置字體的大小
        if (content != null) {
            Font f = new Font("宋體", Font.PLAIN, 25);
            Color mycolor = Color.red;//new Color(0, 0, 255);
            g.setColor(mycolor);
            g.setFont(f);
            g.drawString(content, cx, cy); //表示這段文字在圖片上的位置(cx,cy) .第一個(gè)是你設(shè)置的內(nèi)容。
        }
        g.dispose();
        OutputStream os = new FileOutputStream(outPathWithFileName);
        //創(chuàng)鍵編碼器,用于編碼內(nèi)存中的圖象數(shù)據(jù)。
        JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
        en.encode(buffImg);
        is.close();
        os.close();
    }
}  

實(shí)現(xiàn)給圖片添加水印

某些應(yīng)用場(chǎng)景下需要對(duì)圖片加上水印防止盜用,例如微博用戶圖片。Java中實(shí)現(xiàn)添加水印需要用到BufferedImage、Graphics2D 和ImageIO類。

1. 添加文字水印

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
 * 添加文字水印
 *
 * @author Ricky Fung
 */
public class TextMarkProcessor {
    /**
     * @param args
     */
    public static void main(String[] args) {
        new TextMarkProcessor().testTextMark();
    }
    public void testTextMark() {
        File srcImgFile = new File("D:/test/desktop.png");
        String logoText = "[ 天使的翅膀 ]";
        File outputRotateImageFile = new File("D:/test/desktop_text_mark.jpg");
        createWaterMarkByText(srcImgFile, logoText, outputRotateImageFile, 0);
    }
    public void createWaterMarkByText(File srcImgFile, String logoText,
                                             File outputImageFile, double degree) {
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(srcImgFile);
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = buffImg.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),
                            srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
            if (degree>0) {
                graphics.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getHeight() / 2);
            }
            graphics.setColor(Color.RED);
            graphics.setFont(new Font("宋體", Font.BOLD, 36));
            float alpha = 0.8f;
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));
            graphics.drawString(logoText, buffImg.getWidth()/3, buffImg.getHeight()/2);
            graphics.dispose();
            os = new FileOutputStream(outputImageFile);
            // 生成圖片
            ImageIO.write(buffImg, "JPG", os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

2. 添加圖片水印

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
 * 添加圖片水印
 *
 * @author Ricky Fung
 */
public class PictureMarkProcessor {
    /**
     * @param args
     */
    public static void main(String[] args) {
        new PictureMarkProcessor().testPictureMark();
    }
    public void testPictureMark() {
        File srcImageFile = new File("D:/test/desktop.png");
        File logoImageFile = new File("D:/test/tools.png");
        File outputRoateImageFile = new File("D:/test/desktop_pic_mark.jpg");
        createWaterMarkByIcon(srcImageFile, logoImageFile, outputRoateImageFile, 0);
    }
    public void createWaterMarkByIcon(File srcImageFile, File logoImageFile,
                                             File outputImageFile, double degree) {
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(srcImageFile);
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = buffImg.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),
                            srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
            ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile));
            Image logoImg = logoImgIcon.getImage();
            //旋轉(zhuǎn)
            if (degree>0) {
                graphics.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getWidth() / 2);
            }
            float alpha = 0.8f; // 透明度
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            //水印 的位置
            graphics.drawImage(logoImg, buffImg.getWidth()/3, buffImg.getHeight()/2, null);
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            graphics.dispose();
            os = new FileOutputStream(outputImageFile);
            // 生成圖片
            ImageIO.write(buffImg, "JPG", os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

實(shí)現(xiàn)效果如下:

原圖:

這里寫圖片描述

加文字水?。?/p>

這里寫圖片描述

加圖片水?。?/p>

這里寫圖片描述

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論