图片处理——压缩、缩放与旋转

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;
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值