1. 圖片壓縮
1.1 得到內存輸出(ByteArrayOutputStream)
1.2 得到編碼器,並將內存輸出傳遞給它
1.3 設置編碼參數,(先得到,再設置,最後重新添加給編碼器 )
1.4 編碼
public static OutputStream encode( BufferedImage src, int quality ){
ByteArrayOutputStream imageos = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( imageos );
JPEGEncodeParam params = encoder.getJPEGEncodeParam();
quality = Math.max( 0, Math.min( quality, 100 ) );
params.setQuality( (float)quality/100.0f, false );
encoder.setJPEGEncodeParam( params );
try{
encoder.encode( src );
}catch( IOException e ){
e.printStackTrace();
}
return imageos;
}
2. 圖片的縮放
在網上搜了好多的東西,好幾個版本,但不知道哪個版本好,故將其都粘出來
1. 簡單的resize
private static Image resize( Image src, int width, int height, double factor, boolean propotion ){
BufferedImage image = null;
if( src.getWidth( null ) == -1 ){
return null;
}
if( propotion == true ){
width = (int)(src.getWidth(null)/factor);
height = (int)(src.getHeight(null)/factor);
}
/*
* 據說這種方式僅對JPEG有效?還未進行驗證
* image = src.getScaleInstance( new_w, new_h, Image.SCALE_SMOOTH );
*/
image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
Graphics2D g = image.createGraphics();
g.setColor( Color.white );
g.fillRect( 0, 0, width, height );
g.drawImage(src, 0, 0, width, height, null );
g.dispose();
return image;
}
2. 複雜一點的resize
private static Image resize2( BufferedImage srcImage, int width, int height, double factor, boolean propotion ){
BufferedImage image = null;
if( srcImage.getWidth( null ) == -1 ){
return null;
}
if( propotion == true ){
width = (int)(srcImage.getWidth(null)/factor);
height = (int)(srcImage.getHeight(null)/factor);
}
//查看是否有其它的圖片方式
/*int type = srcImage.getType();
if(type == BufferedImage.TYPE_CUSTOM){
ColorModel cm = srcImage.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(width,
height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
image = new BufferedImage(cm, raster, alphaPremultiplied, null);
}else{
image = new BufferedImage(width, height, type);
}*/
double sx = (double) width / srcImage.getWidth(null);
double sy = (double) height / srcImage.getHeight(null);
image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcImage,
AffineTransform.getScaleInstance(sx, sy));
//另外一種方式
/*g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);*/
//g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return image;
}
對圖片處理以前並沒有接觸過,這三種方式,不知道它的好壞。僅供參考
3. 圖片旋轉
以下代碼轉來的,很好很強大……還有一種方式,我註釋了,比較容易理解
public static BufferedImage rotate( BufferedImage src, double degree, Color bgColor ){
int width = src.getWidth();
int height = src.getHeight();
int w=0, h=0, x=0, y=0;
degree = degree%360;
if( degree < 0 ){
degree = 360 + degree;
}
double angle = Math.toRadians( degree );
/**
*確定旋轉後的圖象的高度和寬度
*/
if(degree == 180 || degree == 0 || degree == 360){
w = width;
h = height;
} else if(degree == 90 || degree == 270){
w = height;
h = width;
}else{
int d = width + height;
w = (int) (d * Math.abs(Math.cos(angle)));
h = (int) (d * Math.abs(Math.sin(angle)));
}
x = (w/2) - (width/2);//確定原點座標
y = (h/2) - (height/2);
BufferedImage image = new BufferedImage( w, h, src.getType() );
Graphics2D g = image.createGraphics();
if( bgColor == null ){
image = g.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
}else{
g.setColor( bgColor );
g.fillRect( 0, 0, w, h );
}
AffineTransform at = new AffineTransform();
at.rotate(angle, w/2, h/2);//旋轉圖象
at.translate(x, y);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
op.filter(src, image);
/*g.fillRect( 0, 0, w, h );
g.rotate( angle, w/2, h/2 );
g.translate( x, y );
g.drawImage( src, 0, 0, null );
g.dispose();*/
return image;
}