我正在使用 Java 进行 Jpeg 压缩来压缩图像,然后在存储之前调整它们的大小。我将存储高度保持为480并根据纵横比计算height,以便保持原始height:width比率相同。
这是我正在使用的代码
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 = " + …Run Code Online (Sandbox Code Playgroud)