2013-06-04 135 views
0

嗨,我正在写一个代码在Java压缩图像文件没有压缩它,但所有我已经能够做的是压缩文件。如何缩小图像文件的大小而不压缩它?

这是我的代码。

package com.test.main; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; 

public class Main { 

    public static final double byteSize = 1024; 
    public static final String successDir = "E://Tmp/S"; 
    public static final String failureDir = "E://Tmp/F"; 
    public static final String tempDir = "E://Tmp/T"; 
    public static final String delimiter = "/"; 
    public static final String zipExtn = ".zip"; 
    public static final String filePath = "E://Tmp/_LND4320HR1.bmp"; 

    public static void main(String[] args) { 
     double fileSizeInMB = 0.0; 
     File inputFile = null; 
     try { 
      inputFile = new File(filePath); 
      if (inputFile.exists()) { 
       fileSizeInMB = ((inputFile.length()/(byteSize))/(byteSize)); 
       if (fileSizeInMB < 6) { 
        writeToDir(inputFile,successDir); 
        System.out.println("Done"); 
       } else { 

        //Compress file 
        compressFile(inputFile); 

        inputFile = new File(tempDir + delimiter 
          + getFileName(inputFile.getName())+ zipExtn); 
        if(inputFile.exists()){ 
         fileSizeInMB = ((inputFile.length()/byteSize)/byteSize); 
         if (fileSizeInMB < 6) { 
          writeToDir(inputFile,successDir); 
          System.out.println("Done"); 
         }else{ 
          writeToDir(inputFile,failureDir); 
          System.out.println("Not Done"); 
         } 
        } 
       } 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static String getFileName(String fileName) { 
     int indexofDot = fileName.lastIndexOf("."); 
     return fileName.substring(0,indexofDot); 
    } 

    public static void writeToDir(File inputFile,String dirPath) throws IOException{ 
     byte[] buffer = new byte[1024]; 
     FileOutputStream fos = new FileOutputStream(dirPath + delimiter 
       + getFileName(inputFile.getName()) + zipExtn); 
     FileInputStream fis = new FileInputStream(inputFile); 
     int length; 

     while ((length = fis.read(buffer)) > 0) { 
      fos.write(buffer, 0, length); 
     } 
     //Closing operations 
     fis.close(); 
     fos.close(); 
    } 
    public static void compressFile(File inputFile) throws IOException{ 
     byte[] buffer = new byte[1024]; 

     FileOutputStream fos = new FileOutputStream(tempDir + delimiter 
       + getFileName(inputFile.getName())+ zipExtn); 
     ZipOutputStream zos = new ZipOutputStream(fos); 
     ZipEntry ze = new ZipEntry(inputFile.getName()); 
     zos.putNextEntry(ze); 
     FileInputStream fis = new FileInputStream(inputFile); 
     int length; 
     while ((length = fis.read(buffer)) > 0) { 
      zos.write(buffer, 0, length); 
     } 
     //Closing operations 
     zos.closeEntry(); 
     zos.close(); 
     fis.close(); 
     fos.close(); 
    } 


} 

上面的代码只能压缩文件如何将此代码转换为压缩但不压缩文件。

+0

有损压缩/无损? – Raptor

+0

任何我只需要转换上述压缩后,如果图像文件高于6MB我需要将其存储到故障文件夹,如果低于6MB需要将其存储在成功的文件夹,我需要能够打开并查看压缩文件。 – user1844638

+0

[ImageIO](http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html)有方法可让您将图像保存为png或jpg – BevynQ

回答

1

该程序将改变图像的大小而不压缩它。

import java.awt.AlphaComposite; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public class ImageTest { 

    private static final int IMG_WIDTH = 512; 
    private static final int IMG_CLAHEIGHT = 512; 

    public static void main(String[] args) { 
     String filePath = "e:\\Image_DCM_PNG\\"; 
     try { 

      File myFile = new File(filePath); 
      File roots[] = myFile.listFiles(); 
      for (int i = 0; i < roots.length; i++) { 
       System.out.println(roots[i]); 
       BufferedImage originalImage = ImageIO.read(new File(filePath + roots[i].getName())); 
       int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); 

       BufferedImage resizeImageBmp = resizeImage(originalImage, type); 
       ImageIO.write(resizeImageBmp, "png", new File(filePath + roots[i].getName())); 
      } 

     } catch (IOException e) { 
      System.out.println(e.getMessage()); 
     } 
    } 

    private static BufferedImage resizeImage(BufferedImage originalImage, int type) { 
     BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_CLAHEIGHT, type); 
     Graphics2D g = resizedImage.createGraphics(); 
     g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_CLAHEIGHT, null); 
     g.dispose(); 
     return resizedImage; 
    } 

    private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type) { 

     BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_CLAHEIGHT, type); 
     Graphics2D g = resizedImage.createGraphics(); 
     g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_CLAHEIGHT, null); 
     g.dispose(); 
     g.setComposite(AlphaComposite.Src); 

     g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
       RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     g.setRenderingHint(RenderingHints.KEY_RENDERING, 
       RenderingHints.VALUE_RENDER_QUALITY); 
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 

     return resizedImage; 
    } 
} 
+0

谢谢你生病检查并让你知道 – user1844638

+0

调整大小或压缩? – Raptor

+0

此代码将调整任何尺寸的图片的大小。 –