我在这里找到一段代码,就是能改变尺寸,而且能加水印的,但我想把水印图片变透明一些,怎么实现
import java.io.File;
import java.io.FileOutputStream;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;/**
* @author chaiwei
* 生成缩略图类
*/
public class JPG {public static void thumbPic(String filename) {
try {
File file = new File(filename);
Image src = javax.imageio.ImageIO.read(file);
File file_sy = new File("D:\\test\\realone.gif");
Image src_sy = javax.imageio.ImageIO.read(file_sy);
int width=src.getWidth(null);
int height=src.getHeight(null);
int new_width = 0;
int new_height = 0;
if (width >= height) {
new_width = 600;
}
else{
new_width = 600;
}
new_height =((new_width * height) / width);
BufferedImage tag = new BufferedImage(new_width,new_height,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_width,new_height,null);
tag.getGraphics().drawImage(src_sy,new_width-src_sy.getWidth(null),new_height-src_sy.getHeight(null),98,40,null);
FileOutputStream out=new FileOutputStream(filename + "_thumb.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
catch(Exception e) {
System.out.println(e);
}
}public static void main(String args[]) {
JPG.thumbPic("D:\\test\\sourse.jpg");
}}
也曾经自己改了一个方法,但不知道为什么不行,总是把图片给弄坏了 public static BufferedImage setOpacity( int opacity ,Image img) {
int width = img.getWidth( null);
int height = img.getHeight( null );
BufferedImage bi = new BufferedImage( width, height,BufferedImage.TYPE_INT_ARGB );
bi.getGraphics().drawImage( img, 0, 0, null );
WritableRaster pixels = bi.getAlphaRaster();
int[] o = new int[ width * height ];
for ( int i = 0; i < o.length; i++ ) {
o[i] = opacity;
}
System.out.println(pixels.getWidth());
for ( int x = 0,m= pixels.getWidth(); x<m; x++ ) {
for ( int y = 0,n =pixels.getHeight();y<n ;y++ ){
pixels.setPixel( x, y, o );
}
}
return bi;
}
import java.io.File;
import java.io.FileOutputStream;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;/**
* @author chaiwei
* 生成缩略图类
*/
public class JPG {public static void thumbPic(String filename) {
try {
File file = new File(filename);
Image src = javax.imageio.ImageIO.read(file);
File file_sy = new File("D:\\test\\realone.gif");
Image src_sy = javax.imageio.ImageIO.read(file_sy);
int width=src.getWidth(null);
int height=src.getHeight(null);
int new_width = 0;
int new_height = 0;
if (width >= height) {
new_width = 600;
}
else{
new_width = 600;
}
new_height =((new_width * height) / width);
BufferedImage tag = new BufferedImage(new_width,new_height,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_width,new_height,null);
tag.getGraphics().drawImage(src_sy,new_width-src_sy.getWidth(null),new_height-src_sy.getHeight(null),98,40,null);
FileOutputStream out=new FileOutputStream(filename + "_thumb.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
catch(Exception e) {
System.out.println(e);
}
}public static void main(String args[]) {
JPG.thumbPic("D:\\test\\sourse.jpg");
}}
也曾经自己改了一个方法,但不知道为什么不行,总是把图片给弄坏了 public static BufferedImage setOpacity( int opacity ,Image img) {
int width = img.getWidth( null);
int height = img.getHeight( null );
BufferedImage bi = new BufferedImage( width, height,BufferedImage.TYPE_INT_ARGB );
bi.getGraphics().drawImage( img, 0, 0, null );
WritableRaster pixels = bi.getAlphaRaster();
int[] o = new int[ width * height ];
for ( int i = 0; i < o.length; i++ ) {
o[i] = opacity;
}
System.out.println(pixels.getWidth());
for ( int x = 0,m= pixels.getWidth(); x<m; x++ ) {
for ( int y = 0,n =pixels.getHeight();y<n ;y++ ){
pixels.setPixel( x, y, o );
}
}
return bi;
}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;public class Water
{
public static void main(String[] args) throws IOException
{
BufferedImage originImg = ImageIO.read(new File("E:/temp/splash.png"));
BufferedImage waterMarkImg = ImageIO.read(new File("E:/temp/1.png"));
addWater(originImg, waterMarkImg, 100, 100, 0.2f);
JFrame f = new JFrame();
f.getContentPane().add(new JLabel(new ImageIcon(originImg)), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
} private static void addWater(BufferedImage originImg, Image waterImg,
int x, int y, float alpha)
{
int waterWid = waterImg.getWidth(null);
int waterHei = waterImg.getHeight(null);
BufferedImage tempImg = new BufferedImage(waterWid, waterHei,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = tempImg.createGraphics();
g2d.drawImage(waterImg, 0, 0, null);
g2d.setColor(new Color(1.0f, 1.0f, 1.0f, alpha));
g2d.setComposite(AlphaComposite.DstIn);
g2d.fillRect(0, 0, waterWid, waterHei);
g2d.dispose();
g2d = originImg.createGraphics();
g2d.drawImage(tempImg, x, y, null);
g2d.dispose();
}
}
ImageIO.write(bufferedImg, "jpg", new File("C:/test.jpg"));不行,保存不了