免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4593 | 回复: 1
打印 上一主题 下一主题

关于jpg图片压缩 小试 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-01 16:56 |只看该作者 |倒序浏览
package org.surpass.test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Test {
        private int wideth;
        private int height;
        private String t = null;

        public void setT(String t) {
                this.t = t;
        }

        public void setWideth(int wideth) {
                // wideth=320;
                this.wideth = wideth;
        }

        public int getWideth() {
                return this.wideth;
        }

        public void setHeight(int height) {
                // height=240;
                this.height = height;
        }

        public int getHeight(int w, int h) // former images size
        {
                // int hhh;
                if (w > wideth) {
                        float ww;
                        ww = (float) w / (float) wideth;
                        float hh = h / ww;
                        return (int) hh;
                } else {
                        this.setWideth(w);
                        return h;
                }

        }

        public void proce(String fpath) throws Exception {
                File _file = new File(fpath);
                Image src = javax.imageio.ImageIO.read(_file);
                int wideth = src.getWidth(null);
                int height = src.getHeight(null);
                int h = this.getHeight(wideth, height);
                BufferedImage tag = new BufferedImage(this.getWideth(), h,
                                BufferedImage.TYPE_INT_RGB);
                Graphics g = tag.getGraphics();
                g.drawImage(src, 0, 0, this.getWideth(), h, null);
                if (t != null) {
                        g.setColor(new Color(242, 242, 242));
                        g.fillRect(this.getWideth() - 120, h - 18, 120, 1;
                        g.setColor(new Color(180, 180, 180));
                        g.drawRect(this.getWideth() - 120, h - 18, 119, 17);
                        g.setColor(new Color(255, 102, 0));
                        g.drawString(t, this.getWideth() - 100, h - 5);
                }
                FileOutputStream out = new FileOutputStream(fpath);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(tag);
                out.close();
        }

        /**
         * 压缩图片方法
         *
         * @param oldFile
         *            将要压缩的图片
         * @param width
         *            压缩宽
         * @param height
         *            压缩长
         * @param quality
         *            压缩清晰度 <b>建议为1.0</b>
         * @param smallIcon
         *            压缩图片后,添加的扩展名
         * @return
         */
        public String proce1(String oldFile, int width, int height, float quality,
                        String smallIcon) {
                if (oldFile == null) {
                        return null;
                }
                String newImage = null;
                try {
                        File file = new File(oldFile);
                        if (!file.exists()) // 文件不存在时
                                return null;
                        /** 对服务器上的临时文件进行处理 */
                        Image srcFile = ImageIO.read(file);

                        /** 宽,高设定 */
                        BufferedImage tag = new BufferedImage(width, height,
                                        BufferedImage.TYPE_INT_RGB);
                        tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
                        String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
                        /** 压缩后的文件名 */
                        // newImage =smallIcon + filePrex
                        // +oldFile.substring(filePrex.length());
                        newImage = filePrex + smallIcon
                                        + oldFile.substring(filePrex.length());
                        // newImage = smallIcon;
                        /** 压缩之后临时存放位置 */
                        FileOutputStream out = new FileOutputStream(newImage);

                        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                        JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
                        /**
                         * 压缩质量 创建替代当前已建量化表的新量化表。它也将 Component QTable 映射更新为当前已编码 COLOR_ID
                         * 的缺省值。根据质量参数的不同,创建的量化表介于压缩比很高但质量很差 (0.0) 与压缩比低但质量很好 (1.0) 之间。
                         *
                         * 在 1.0 的质量水平,表的值将都是 1。这样就不会由于量化而丢失数据,但色度二次抽样(如果使用)和 DCT
                         * 中的舍入误差会在某种程度上降低图像质量。
                         *
                         * 以下为标准 Chrominance Q-Table 的线性操作。
                         *
                         * Some guidelines: 0.75 high quality 0.5 medium quality 0.25 low
                         * quality
                         */
                        jep.setQuality(quality, true);
                        encoder.encode(tag, jep);

                        out.close();
                        srcFile.flush();

                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return newImage;
        }

        //
        public static void main(String str[]) {
                Test ps = new Test();
                try {
                        System.out.println(ps.proce1("d:/temp/test.jpg", 450, 500,
                                        0.6f, "1");
                        System.out.println(ps.proce1("d:/temp/test.jpg", 450, 500,
                                        1, "2");
                        System.out.print("成功哦";
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}

论坛徽章:
0
2 [报告]
发表于 2010-07-02 11:17 |只看该作者
完善在proce1方法中加入以下代码,来修改压缩比例参数.控制在0.0-1.0范围内:
         if(quality<0.0f || quality>1.0f){
                                quality=1f;
                        }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP