展会信息港展会大全

新的图像压缩算法,提高了性能
来源:互联网   发布日期:2011-09-27 12:14:51   浏览:7698次  

导读:新的图像压缩算法,提高了性能_开心就好_新浪博客,开心就好,...

正文 字体大小: 中

新的图像压缩算法,提高了性能 (2006-09-15 08:42:37)

package src;

import java.awt.image.BufferedImage; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException;

import com.mullassery.imaging.Imaging; import com.mullassery.imaging.ImagingFactory; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class NewScaleImage {

 int width = 0;

 int height = 0;

 private String destFile;  private String srcFileName;

 BufferedImage srcBufferImage;

 private int[] rgbArray = null;

 private static Imaging tool = new ImagingFactory()    .createImagingInstance(ImagingFactory.JAVA2D);

 public NewScaleImage(String fileName) throws IOException {   this(new File(fileName));  }

 public NewScaleImage(File file) throws IOException {   String srcFile = file.getName();   String srcFilePath = file.getAbsolutePath();   this.destFile = srcFile.substring(0, srcFile.lastIndexOf(".")) + ".jpg";   //        img = javax.imageio.ImageIO.read(file);   srcBufferImage = tool.read(new File(srcFilePath));   //        ImageIO.write((RenderedImage) srcBufferImage, "bmp", new File("E:\\temp1\\testSrc.bmp"));   width = srcBufferImage.getWidth();   height = srcBufferImage.getHeight();  }  public NewScaleImage() throws IOException{

 }

 public BufferedImage scaleImage(int outWidth, int outHeight) {   int width = srcBufferImage.getWidth();   int height = srcBufferImage.getHeight();   rgbArray = srcBufferImage.getRGB(0, 0, width, height, null, 0, width);   if (DetermineResultSize(outWidth, outHeight) == 1) {    return srcBufferImage;   }

  BufferedImage pbFinalOut = new BufferedImage(outWidth, outHeight,     BufferedImage.TYPE_INT_RGB);

  double hScale = ((double) width) / ((double) outWidth);   double vScale = ((double) height) / ((double) outHeight);

  int winX0, winY0, winX1, winY1;   int valueRGB = 0;   long R, G, B;   int x, y, i, j;   int n;

  for (y = 0; y < outHeight; y++) {             System.out.println("y :"+ y);    winY0 = (int) (y * vScale + 0.5);    if (winY0 < 0) {     winY0 = 0;    }    winY1 = (int) (winY0 + vScale + 0.5);    if (winY1 > height) {     winY1 = height;    }    System.out.println("winY0 :"+ winY0);    System.out.println("winY1 :"+ winY1);

   for (x = 0; x < outWidth; x++) {     System.out.println("   x :"+ x);     winX0 = (int) (x * hScale + 0.5);     if (winX0 < 0) {      winX0 = 0;     }     winX1 = (int) (winX0 + hScale + 0.5);     if (winX1 > width) {      winX1 = width;     }     System.out.println("          winX0 :"+ winX0);     System.out.println("          winX1 :"+ winX1);

    R = 0;     G = 0;     B = 0;     for (i = winX0; i < winX1; i++) {      System.out.println("                   i :"+ i);      for (j = winY0; j < winY1; j++) {       valueRGB = rgbArray[width * j + i];

      R += GetRedValue(valueRGB);       G += GetGreenValue(valueRGB);       B += GetBlueValue(valueRGB);       System.out.println(" reb->                     j :"+ j);

     }     }     n = (winX1 - winX0) * (winY1 - winY0);     R = (int) (((double) R) / n + 0.5);     G = (int) (((double) G) / n + 0.5);     B = (int) (((double) B) / n + 0.5);

    valueRGB = ComRGB(Clip((int) R), Clip((int) G), Clip((int) B));     pbFinalOut.setRGB(x, y, valueRGB);    }   }

  return pbFinalOut;  }

 /**   * 决定图像尺寸   */  public int DetermineResultSize(int w, int h) {   double scaleH, scaleV;   scaleH = (double) w / (double) width;   scaleV = (double) h / (double) height;

  // 判断一下scaleH,scaleV,不做放大操作   if (scaleH >= 1.0 && scaleV >= 1.0) {    return 1;   }

  return 0;

 } // end of DetermineResultSize()

 int Clip(int x) {   if (x < 0)    return 0;   if (x > 255)    return 255;   return x;  }

 private int GetRedValue(int rgbValue) {   int temp = rgbValue & 0x00ff0000;   return temp >> 16;  }

 private int GetGreenValue(int rgbValue) {   int temp = rgbValue & 0x0000ff00;   return temp >> 8;  }

 private int GetBlueValue(int rgbValue) {   return rgbValue & 0x000000ff;  }

 private int ComRGB(int redValue, int greenValue, int blueValue) {

  return (redValue << 16) + (greenValue << 8) + blueValue;  }

 public void resize(int w, int h) throws IOException {

  File srcFile =  new File(srcFileName);   srcBufferImage = tool.read(srcFile);   width = srcBufferImage.getWidth();   height = srcBufferImage.getHeight();

  rgbArray = srcBufferImage.getRGB(0, 0, width, height, null, 0, width);   BufferedImage _image = scaleImage(w, h);

  FileOutputStream out = new FileOutputStream(destFile);   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

  encoder.encode(_image);   out.close();  }

 public void resizeByWidth(int w) throws IOException {   float h = ((float) height * w / width);   resize(w, new Float(h).intValue());  }

 public void resizeByHeight(int h) throws IOException {   float w = ((float) width * h / height);   resize(new Float(w).intValue(), h);  }

 public void resizeFix(int w, int h) throws IOException {   if (width <= w && height <= h) {    resize(width, height);    return;   }

  if ((float) width / height > (float) w / h) {    resizeByWidth(w);   } else {    resizeByHeight(h);   }  }

 public void setDestFile(String fileName) {   destFile = fileName;  }

 public String getDestFile() {   return destFile;  }

 public static void main(String[] args) {

  long cycleStartTime = System.currentTimeMillis();   long cycleEndTime = System.currentTimeMillis();   try {    FileOutputStream file1 = new FileOutputStream(      "E:\\temp4\\result_newJava.txt");    DataOutputStream dos = new DataOutputStream(file1);    NewScaleImage thum = new NewScaleImage();    for (int i = 5; i < 7; i++) {     String srcName = "test" + i;     String thumbnailFile = "";     try {      cycleStartTime = System.currentTimeMillis();      thum.setSrcFileName("E:\\temp4\\"        + srcName + ".jpg");      thumbnailFile = "E:\\temp4\\" + srcName         + "_400x400.jpg";      thum.setDestFile(thumbnailFile);      thum.resizeFix(400, 400);

     thum.setSrcFileName("E:\\temp4\\"        + srcName + "_400x400.jpg");      thumbnailFile = "E:\\temp4\\" + srcName         + "_200x200.jpg";      thum.setDestFile(thumbnailFile);      thum.resizeFix(200, 200);

     thum.setSrcFileName("E:\\temp4\\"        + srcName + "_400x400.jpg");      thumbnailFile = "E:\\temp4\\" + srcName         + "_100x100.jpg";      thum.setDestFile(thumbnailFile);      thum.resizeFix(100, 100);

     thum.setSrcFileName("E:\\temp4\\"        + srcName + "_400x400.jpg");      thumbnailFile = "E:\\temp4\\" + srcName         + "_50x50.jpg";      thum.setDestFile(thumbnailFile);      thum.resizeFix(50,50);

     cycleEndTime = System.currentTimeMillis();      dos.writeBytes(i + ":" + (cycleEndTime - cycleStartTime));      dos.writeBytes("\r\n");      System.out.println(i + ":"        + (cycleEndTime - cycleStartTime));     } catch (Exception e) {      // TODO: handle exception     }    }    dos.close();    file1.close();   } catch (Exception e) {    // TODO: handle exception   }   System.out.println("finish;");  }

 public String getSrcFileName() {   return srcFileName;  }

 public void setSrcFileName(String srcFileName) {   this.srcFileName = srcFileName;  } }

分享

阅读┊ ┊ ┊┊ ┊打印┊

加载中,请稍候......

前一篇:jar包中properties文件的读取

后一篇:ANT十五大最佳实践

评论 重要提示:警惕虚假中奖信息       在插画中找寻曾经的美好       关注每日最热门博客 

  • 评论加载中,请稍候...
  • 发评论 旅游行摄,轻品日本       是谁改变了你的博客?       关注每日最热门博客

    登录名: 密码: 找回密码 注册

    昵   称:

       

    验证码: 请点击后输入验证码

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

    < 前一篇jar包中properties文件的读取

    后一篇 >ANT十五大最佳实践

    赞助本站

    AiLab云推荐
    推荐内容
    展开

    热门栏目HotCates

    Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港