在 CodenameOne 中调整照片大小而不会丢失正确的大小

Resize a photo without loosing the correct size in CodenameOne

我正在使用此代码拍照

m_photoFile = Capture.capturePhoto(1920, -1);

CN1 不强制使用 1920 宽度,至少在 Android 手机中是这样。所以我需要手动调整图像大小并存储图像以符合我的 1920x1080 要求。

如果我尝试这样做,我的照片会变得乱七八糟。

photoFileImage = photoFileImage.subImage(0, 0, 1920, 1080, false);
OutputStream fos = fss.openOutputStream(m_photoFile );
imageIO.save(photoFileImage, fos, ImageIO.FORMAT_JPEG, 1);

我希望调整我的照片大小并裁剪多余部分... 因此,如果我的宽度为 1920x1100,那些额外的 20 像素高度,我希望它们被裁剪......如果顶部有 10 个,底部有 10 个......

我不确定我该怎么做...

请帮忙 谢谢!

试试这个:

String m_photoFile = Capture.capturePhoto();
Image image = Image.createImage(m_photoFile);
if (image.getWidth() > image.getHeight()) {
    image = image.scaledLargerRatio(1920, 1080);
    photoFileImage = image.subImage((int) (image.getWidth() - 1080) / 2, 0, image.getHeight(), image.getHeight(), true);
} else {
    image = image.scaledLargerRatio(1080, 1920);
    photoFileImage = image.subImage(0, (int) (image.getHeight() - 1080) / 2, image.getWidth(), image.getWidth(), true);
}