`
zhangweioak
  • 浏览: 61883 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java图片操作

    博客分类:
  • Java
 
阅读更多
1、图片压缩

//图片压缩
public static void imageCompress(String path,String newPath,String fileName,String toFileName,float scale,int width,int height){
        imageCompress(path,newPath, fileName, toFileName, scale, 0.75f, width, height);
    }
    /**
     * 描述:
     * @param path 需要压缩的图片路径
     * @param fileName 要压缩的图片名称
     * @param toFileName 压缩后的图片名称
     * @param scale 压缩比例 不能大于1,默认0.5
     * @param quality 压缩品质介于0.1~1.0之间 
     * @param width 压缩后的图片的宽度
     * @param height 压缩后的图片的高度
     * 返回值:void
     */
    private static void imageCompress(String path,String newPath, String fileName,String toFileName,float scale,float quality,int width,int height){
    	FileOutputStream out = null;
    	try {
            Image image = javax.imageio.ImageIO.read(new File(path +"/"+ fileName));
            int imageWidth = image.getWidth(null);
            int imageHeight = image.getHeight(null);
            if(scale >=1.0) scale = 0.99f;//默认压缩比为0.5,压缩比越大,对内存要去越高,可能导致内存溢出
            //按比例计算出来的压缩比
            float realscale = getRatio(imageWidth,imageHeight,width,height);
            float finalScale = Math.min(scale, realscale);//取压缩比最小的进行压缩
            imageWidth = (int)(finalScale*imageWidth);
            imageHeight = (int)(finalScale*imageHeight);
            
            image = image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_AREA_AVERAGING);
            BufferedImage mBufferedImage = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = mBufferedImage.createGraphics();
    
            g2.drawImage(image, 0, 0,imageWidth, imageHeight, Color.white,null);
            g2.dispose();
            
            float[] kernelData2 = {-0.125f, -0.125f, -0.125f,-0.125f,2, -0.125f,-0.125f,-0.125f, -0.125f };
            Kernel kernel = new Kernel(3, 3, kernelData2);
            ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
            mBufferedImage = cOp.filter(mBufferedImage, null);

            out =  new FileOutputStream(newPath+"/" + toFileName);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(mBufferedImage);
            param.setQuality(quality, true);//默认0.75
            encoder.setJPEGEncodeParam(param);
            encoder.encode(mBufferedImage);
        }catch (FileNotFoundException fnf){
        	fnf.printStackTrace();
        }catch (IOException ioe){
            ioe.printStackTrace();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
        	 try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
      }
    
   private static float getRatio(int width,int height,int maxWidth,int maxHeight){
          float Ratio = 1.0f;
          float widthRatio ;
          float heightRatio ;
        widthRatio = (float)maxWidth/width;
        heightRatio = (float)maxHeight/height;
        if(widthRatio<1.0 || heightRatio<1.0){
          Ratio = widthRatio<=heightRatio?widthRatio:heightRatio;
        }
        return Ratio;
      }
   
    /**
     * 动态获取压缩比列根据宽度
     * @param map
     * @return
     */
    public static float getCompressSize(float width){
    	float result = 0.5f;
    	result = 595/width;
    	result = Float.parseFloat(StringUtil.formatNumber("#.##", result));
    	return result;
    }

    /** 
     * 计算图片尺寸大小等信息:w宽、h高、s大小。异常时返回null。 
     * 
     * @param imgpath 图片路径 
     * @return 图片信息map 
     */ 
    public static Map getImgInfo(InputStream fis) throws Exception { 
            Map map = new HashMap();
            BufferedImage buff = ImageIO.read(fis); 
            map.put("w", new Long(buff.getWidth()* 1L)); //宽度
            //map.put("h", new Long(buff.getHeight()* 1L)); //高度
            //map.put("s", new Long(imgfile.length()));  //获取图片大小
            fis.close(); 
            return map; 
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics