2016-11-29 82 views
2

我壓縮圖像在Java中執行Jpeg壓縮,然後在存儲之前調整它們的大小。我保持存儲高度爲480並根據縱橫比計算出height,所以保持原來的height:width比例相同。Java:getHeight()在BufferedImage返回圖像寬度和getWidth()返回圖像高度

這是我使用

String inputImagePath = "1.JPG"; 
String outputImagePath = "Test Compression\\" + "1.JPG"; 

File imageFile = new File(inputImagePath); 
File compressedImageFile = new File(outputImagePath); 

int height = 640; 

System.out.print("Conversion Start!\n"); 

if (imageFile != null) 
{ 

    InputStream is = new FileInputStream(imageFile); 
    OutputStream os = new FileOutputStream(compressedImageFile); 

    float quality = 0.2 f; 

    BufferedImage image = ImageIO.read(is); 

    double aspectRatio = (double) image.getWidth()/image.getHeight(); 

    width = (int)(height * aspectRatio); 
    System.out.println("Original height = " + image.getHeight() + " Original width = " + image.getWidth()); 
    System.out.println(" aspect ratio = " + aspectRatio); 
    System.out.println("height = " + height + " width = " + width + " aspect ratio = " + aspectRatio); 

    BufferedImage resizedImage = new BufferedImage(width, height, image.getType()); 
    Graphics2D g = resizedImage.createGraphics(); 
    g.drawImage(image, 0, 0, width, height, null); 
    g.dispose(); 

    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); 

    if (!writers.hasNext()) 
     throw new IllegalStateException("No writers found"); 

    ImageWriter writer = (ImageWriter) writers.next(); 
    ImageOutputStream ios = ImageIO.createImageOutputStream(os); 
    writer.setOutput(ios); 

    ImageWriteParam param = writer.getDefaultWriteParam(); 
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 
    param.setCompressionQuality(quality); 

    writer.write(null, new IIOImage(resizedImage, null, null), param); 
} 

System.out.print("Conversion compete!"); 

在這裏,代碼在圖像元數據

Here width = 1920, height = 2560

這裏是打印內容:

Original height = 1920 Original width = 2560 
aspect ratio = 1.3333333333333333 

height = 480 width = 640 
aspect ratio = 1.3333333333333333 

我所施加的代碼,以其他圖像,我實際上有width > height沒有輪換問題。這個rotaion問題只發生在height > width的圖像上據我所知,在我的代碼中沒有錯,我必須丟失與getHeight()和getWidth()函數有關的東西。請幫助我

+0

你能更新你的身高變量您的代碼示例中呢?它似乎失蹤了。 – Flipbed

+0

我用'height'變量更新代碼。順便提一下,Sergey Grinev建議使用[元數據提​​取器](https://github.com/drewnoakes/metadata-extractor)獲得期望的結果。我把它貼在下面。 –

回答

1

以備將來參考,我通過謝爾蓋Grinev給出修改的代碼一點點引用了,並得到想要的結果。下面是代碼

Metadata metadata = ImageMetadataReader.readMetadata(imageFile); 
 
Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); 
 
\t \t \t 
 
int orientation = 1; 
 
try{ 
 
orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION); 
 
} catch (MetadataException me) { 
 
System.out.println("Could not get orientation"); 
 
} 
 

 
int orgWidth = image.getWidth(); 
 
int orgHeight = image.getHeight(); 
 
\t \t \t 
 
double orgAspectRatio = (double)orgWidth/orgHeight; 
 
\t \t \t 
 
\t \t \t 
 
AffineTransform affineTransform = new AffineTransform(); 
 
AffineTransformOp affineTransformOp; 
 
BufferedImage orientedImage = new BufferedImage(image.getHeight(), image.getWidth(), image.getType()); 
 
\t \t 
 
boolean rotation = false; 
 
\t   
 
switch (orientation) { 
 
    case 1: 
 
     \t orientedImage = image; 
 
     break; 
 
    case 2: // Flip X 
 
     affineTransform.scale(-1.0, 1.0); 
 
     affineTransform.translate(-orgWidth, 0); 
 
    \t affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR); 
 
\t  orientedImage = affineTransformOp.filter(image, orientedImage); 
 
\t  break; 
 
    case 3: // PI rotation 
 
      affineTransform.translate(orgWidth, orgHeight); 
 
      affineTransform.rotate(Math.PI); 
 
      affineTransformOp = new AffineTransformOp(affineTransform,AffineTransformOp.TYPE_BILINEAR); 
 
     orientedImage = affineTransformOp.filter(image, orientedImage); 
 
     break; 
 
    case 4: // Flip Y 
 
      affineTransform.scale(1.0, -1.0); 
 
      affineTransform.translate(0, -orgHeight); 
 
\t \t affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR); 
 
      orientedImage = affineTransformOp.filter(image, orientedImage); 
 
      break; 
 
    case 5: // - PI/2 and Flip X 
 
     \t rotation = true; 
 
      affineTransform.rotate(-Math.PI/2); 
 
      affineTransform.scale(-1.0, 1.0); 
 
\t \t \t affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR); 
 
      orientedImage = affineTransformOp.filter(image, orientedImage); 
 
      break; 
 
    case 6: // -PI/2 and -width 
 
     \t rotation = true; 
 
      affineTransform.translate(orgHeight, 0); 
 
      affineTransform.rotate(Math.PI/2); 
 
\t \t \t affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR); 
 
      orientedImage = affineTransformOp.filter(image, orientedImage); 
 
      break; 
 
    case 7: // PI/2 and Flip 
 
     \t rotation = true; 
 
      affineTransform.scale(-1.0, 1.0); 
 
      affineTransform.translate(-orgHeight, 0); 
 
      affineTransform.translate(0, orgWidth); 
 
      affineTransform.rotate(3 * Math.PI/2); 
 
\t \t \t affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR); 
 
      orientedImage = affineTransformOp.filter(image, orientedImage); 
 
      break; 
 
    case 8: // PI/2 
 
     \t rotation = true; 
 
      affineTransform.translate(0, orgWidth); 
 
      affineTransform.rotate(3 * Math.PI/2); 
 
\t \t \t affineTransformOp = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR); 
 
      orientedImage = affineTransformOp.filter(image, orientedImage); 
 
      break; 
 
    default: 
 
     \t orientedImage = image; 
 
      break; 
 
     }  
 
\t   
 
double aspectRatio; 
 
\t   
 
if(rotation){ 
 
    \t aspectRatio = (double)orientedImage.getWidth()/orientedImage.getHeight(); 
 
     \t width = outWidth; 
 
     \t height = (int)(width/aspectRatio); 
 
     } 
 
     else{ 
 
     \t aspectRatio = (double)orientedImage.getWidth()/orientedImage.getHeight(); 
 
      \t width = (int)(height*aspectRatio); 
 
     }