Jpeg圧縮をJavaで行い、保存する前にサイズを変更しています。私はストレージの高さを480
とし、アスペクト比に基づいてheight
を計算するので、元のままにしておきます。height:width
比率を同じにします。Java:BufferedImageのgetHeight()がイメージの幅を返しています。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!");
を使用していたコードは、ここでここで画像メタデータ
が印刷コンテンツですされています
Original height = 1920 Original width = 2560
aspect ratio = 1.3333333333333333
height = 480 width = 640
aspect ratio = 1.3333333333333333
私はコードへの適用実際にwidth > height
と私が持っていた他の画像回転の問題はありません。 height > width
の画像にはこの回転問題のみが発生しています。私の知る限りでは、コードに間違いがないことを知っていれば、getHeight()とgetWidth()関数に関連するものが欠けているはずです。助けてください
はありますか?それは見当たりません。 – Flipbed
私は 'height'変数でコードを更新します。 Sergey Grinevが提案したように、[metadata extractor](https://github.com/drewnoakes/metadata-extractor)を使用して望みの結果が得られました。私はそれを下に掲載しました。 –