六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

图片压缩

[复制链接]

升级  60%

38

主题

38

主题

38

主题

秀才

Rank: 2

积分
140
 楼主| 发表于 2013-1-15 02:52:02 | 显示全部楼层 |阅读模式
/**     * 图片压缩。     * @param formatWideth 图片宽度     * @param formatHeight 图片高度     * @param fpath 临时存放路径     * @param filename 源图片名     * @param out 压缩后的图片名     * @param texts  图片的后缀名     * @return FileInputStream 对象     * @throws PhotoManageException  PhotoManageException     */    public static byte[] jMagicCompress(float formatWideth, float formatHeight, String fpath, String filename,        String out, String texts)        throws PhotoManageException    {        RunLogger.info("FileInputStream start");        byte[] bytes = null;                //源图片的完整路径        String imgpath = fpath + filename;        File file = new File(imgpath);        BufferedImage bis = null;                try        {                        bis = ImageIO.read(file);                        if (null == bis)            {                throw new PhotoManageException();            }            bis.flush();            int imageWideth = bis.getWidth();            int imageHeight = bis.getHeight();            if (imageWideth > formatWideth || imageHeight > formatHeight)            {                bytes = resize(formatWideth, formatHeight, imageWideth, imageHeight, imgpath);            }            else            {                bytes = getByteFromFile(file);            }            // bis.getAlphaRaster().getDataBuffer().        }        catch (IOException e1)        {            RunLogger.error("BuffredImage read failed", e1);                    }                return bytes;    }/**     * 按比例压缩图片     * @param formatWideth 压缩后的图片宽度     * @param formatHeight 压缩后的图片高度     * @param imageWideth 图片的真实宽度     * @param imageHeight 图片的真实高度     * @param oldImgPath     * @return byte[] 返回图片的字节数组     */    private static byte[] resize(float formatWideth, float formatHeight, int imageWideth, int imageHeight,        String oldImgPath)    {        byte[] bytes = null;        // 宽度比例        float prw = imageWideth / formatWideth;                // 高度比例        float prh = imageHeight / formatHeight;                // 压缩比例        float pr = prh;        if (formatHeight == PortalConstants.SMALLFORMATWIDTH)        {            if (prw < prh)            {                pr = prw; //按比例小的压缩                            }        }        else        {            if (prw > prh)            {                pr = prw; //按比例大的压缩                            }        }        if (pr < 1)        {            pr = 1;        }        String s1 = String.valueOf(imageWideth / pr);        String s2 = String.valueOf(imageHeight / pr);        int nw = Integer.parseInt(s1.substring(0, s1.indexOf(".")));        int nh = Integer.parseInt(s2.substring(0, s2.indexOf(".")));        if (nw < 1 || nh < 1)        {            nw = 1;            nh = 1;        }                try        {            bytes = PhotoManageBean.getInstance().resize(oldImgPath, nw, nh);        }        catch (Exception e)        {            RunLogger.error("RESIZE METHOD  EXCEPTION", e);                    }        return bytes;    }/** * 图片管理。 * @author  alvin * @version  [版本号, May 8, 2009] * @see  [相关类/方法] * @since  DPFV100R001C01B010 */public final class PhotoManageBean{        private static PhotoManageBean instance = new PhotoManageBean();        private final int half = 2;        private PhotoManageBean()    {    }        public static PhotoManageBean getInstance()    {        return instance;    }/**     * 图片压缩     * @param fromFileStr 源文件路径     * @param changeToWideth 压缩后的文件宽度     * @param changeToHeight 压缩后的文件高度     * @return boolean 值     * @see [类、类#方法、类#成员]     */    public byte[] resize(String fromFileStr, int changeToWideth, int changeToHeight)    {        RunLogger.info("resize start");        MagickImage image = null;        //ByteArrayOutputStream os = null;        byte[] outblob = null;        try        {            System.setProperty("jmagick.systemclassloader", "no");            ImageInfo info;            DebugLogger.inf("Image file:" + fromFileStr, null);            info = new ImageInfo(fromFileStr);                        image = new MagickImage(info);                        MagickImage scaled = image.scaleImage(changeToWideth, changeToHeight);//小图片文件的大小.            //            scaled.setFileName(saveToFileStr);            //            scaled.writeImage(info);                        outblob = scaled.imageToBlob(new ImageInfo());            //  DebugLogger.inf("=====================     " + outblob);            // byte[] outblob = scaled.imageToBlob(info);             DebugLogger.inf("Image size after compress:width=" + changeToWideth + ",height=" + changeToHeight, null);            //            os = new ByteArrayOutputStream();            //            os.write(outblob);            //            os.flush();            //                    }        catch (MagickException e)        {            RunLogger.error("File resize MagickException", e);                    }        catch (Exception e)        {            RunLogger.error("File resize abnormity", e);                    }        finally        {            if (null != image)            {                image.destroyImages();                image = null;            }        }                return outblob;    }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表