java實現給圖片添加浮水印和文字

來源:互聯網
上載者:User
import java.io.*;
import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.*;
import javax.swing.*;


public class Test...{
    public static void main(String[]args) throws Exception...{
        
        //1.jpg是你的 主圖片的路徑
        InputStream is = new FileInputStream("1.jpg");
        
        
        //通過JPEG圖象流建立JPEG資料流解碼器
        JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
        //解碼當前JPEG資料流,返回BufferedImage對象
        BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
        //得到畫筆對象
        Graphics g = buffImg.getGraphics();
        
        //建立你要附加的圖象。
        //2.jpg是你的小圖片的路徑
        ImageIcon imgIcon = new ImageIcon("2.jpg"); 
        
        //得到Image對象。
        Image img = imgIcon.getImage();
        
        //將小圖片繪到大圖片上。
        //5,300 .表示你的小圖片在大圖片上的位置。
        g.drawImage(img,5,330,null);
        
        
        
        //設定顏色。
        g.setColor(Color.BLACK);
        
        
        //最後一個參數用來設定字型的大小
        Font f = new Font("宋體",Font.BOLD,30);
        
        g.setFont(f);
        
        //10,20 表示這段文字在圖片上的位置(x,y) .第一個是你設定的內容。
        g.drawString("默哀555555。。。。。。。",10,30);
        
        g.dispose();
        
        
        
        OutputStream os = new FileOutputStream("union.jpg");
        
        //創鍵編碼器,用於編碼記憶體中的圖象資料。
        
        JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
        en.encode(buffImg);
        
        
        is.close();
        os.close();
        
        System.out.println ("合成結束。。。。。。。。");
        
        
    }    
    
}

//圖片的為位置控制:    修改代碼中的: g.drawImage(img,5,330,null);  5(x) 和 330(y)
//內容和字顯示的位置:      g.drawString("默哀555555。。。。。。。",10,30);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.