当前位置: 技术问答>java相关
如何改变图片大小?很紧急,希望大家能帮忙!!!
来源: 互联网 发布时间:2015-05-11
本文导语: 就像photoshop中缩小功能一样,将1280*1024的图片改成800*600! | 测试成功。 添加包: import com.sun.image.codec.jpeg.*; import java.awt.image.BufferedImage; import java.awt.image.Kernel; import java.awt.image.BufferedImageOp;...
就像photoshop中缩小功能一样,将1280*1024的图片改成800*600!
|
测试成功。
添加包:
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.BufferedImageOp;
import java.awt.image.*;
添加类成员:
private InputStream inputStream;
private ByteArrayOutputStream bout;
private BufferedImage bufferedImage;
ImageIcon mi = new ImageIcon("E:\samples\java\SailiTest\T18Big.gif");
this.saveImage(mi.getImage(),"e:\2.jpg");//存成800*600的图像
//转换方法
public void saveImage(Image pImage,String ofileDirAndName)
{
BufferedImage bimg = null;
int w = pImage.getWidth(null);
int h = pImage.getHeight(null);
int [] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(pImage,0, 0, w, h,pixels, 0, w);
try {
pg.grabPixels();
}
catch(InterruptedException e) {
e.printStackTrace();
}
this.bout= new ByteArrayOutputStream();System.out.println("3");
System.out.println("图像宽高:" +w+":"+h );
BufferedImage bufferedImage= new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
bufferedImage.setRGB(0,0,800,600,pixels,0,w);
float[] sharpKernel = { 0.0f, -1.0f, 0.0f,-1.0f, 5.0f, -1.0f, 0.0f, -1.0f, 0.0f };System.out.println("5");
BufferedImageOp sharpen= new ConvolveOp( new Kernel(3, 3, sharpKernel), ConvolveOp.EDGE_NO_OP, null);
BufferedImage sharp= sharpen.filter( bufferedImage, null );
try
{
JPEGEncodeParam jpegParam= JPEGCodec.getDefaultJPEGEncodeParam( sharp );
jpegParam.setQuality( 1.0f, false );
JPEGImageEncoder jpeg= JPEGCodec.createJPEGEncoder( bout, jpegParam );
jpeg.encode( bufferedImage );
this.bout.writeTo(new FileOutputStream(ofileDirAndName));
}
catch(Exception ee)
{
System.err.println("出错了"+ee);
}
}
添加包:
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.BufferedImageOp;
import java.awt.image.*;
添加类成员:
private InputStream inputStream;
private ByteArrayOutputStream bout;
private BufferedImage bufferedImage;
ImageIcon mi = new ImageIcon("E:\samples\java\SailiTest\T18Big.gif");
this.saveImage(mi.getImage(),"e:\2.jpg");//存成800*600的图像
//转换方法
public void saveImage(Image pImage,String ofileDirAndName)
{
BufferedImage bimg = null;
int w = pImage.getWidth(null);
int h = pImage.getHeight(null);
int [] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(pImage,0, 0, w, h,pixels, 0, w);
try {
pg.grabPixels();
}
catch(InterruptedException e) {
e.printStackTrace();
}
this.bout= new ByteArrayOutputStream();System.out.println("3");
System.out.println("图像宽高:" +w+":"+h );
BufferedImage bufferedImage= new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
bufferedImage.setRGB(0,0,800,600,pixels,0,w);
float[] sharpKernel = { 0.0f, -1.0f, 0.0f,-1.0f, 5.0f, -1.0f, 0.0f, -1.0f, 0.0f };System.out.println("5");
BufferedImageOp sharpen= new ConvolveOp( new Kernel(3, 3, sharpKernel), ConvolveOp.EDGE_NO_OP, null);
BufferedImage sharp= sharpen.filter( bufferedImage, null );
try
{
JPEGEncodeParam jpegParam= JPEGCodec.getDefaultJPEGEncodeParam( sharp );
jpegParam.setQuality( 1.0f, false );
JPEGImageEncoder jpeg= JPEGCodec.createJPEGEncoder( bout, jpegParam );
jpeg.encode( bufferedImage );
this.bout.writeTo(new FileOutputStream(ofileDirAndName));
}
catch(Exception ee)
{
System.err.println("出错了"+ee);
}
}