Chuck
Chuck

Reputation: 245

Resize a photo without loosing the correct size in CodenameOne

I'm capturing a photo using this code

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

CN1 is not enforcing the 1920 width, at least in Android handsets. So I need to manually resize and store the Image to match my 1920x1080 requirement.

If I try this, my pictures are getting disorted.

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

I want my picture to be resized and any excess croped... So if I have a width of 1920x1100 those extra 20 pixels in the height, I want them croped... if posible 10 at the top, 10 at the bottom....

I'm not positive on how I could do that...

Please help Thanks!

Upvotes: 3

Views: 123

Answers (1)

Diamond
Diamond

Reputation: 7483

Try this:

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);
}

Upvotes: 2

Related Questions