android - How to take a photo of the same aspect ratio as in preview size? -
android - How to take a photo of the same aspect ratio as in preview size? -
99.9% of answers on question in title follows:
when searching through list<camera.size>
, provided camera.parameters#getsupportedpicturesizes() seek find size of same aspect ratio camera's preview size set via camera.parameters.html#setpreviewsize(int,int), or close possible.
that's fine, if can't find image size of the same aspect ratio in preview size (android gives no guarantee every preview size supported photographic camera there image size of same aspect ratio), , other image sizes (which as close possible) stretch photo in this , this questions? how can create photo of same aspect ratio in preview, if image size , preview size set in camera.parameters have different aspect ratios?
we can larn general approach issue in this answer. short quote:
my solution first scale preview selection rectangle native photographic camera image size. then, know area of native resolution contains content want, can similar operation scale rectangle on native resolution smaller image captured per camera.parameters.setpicturesize.
now, actual code. easiest way perform scaling utilize matrix. has method matrix#setrecttorect(android.graphics.rectf, android.graphics.rectf, android.graphics.matrix.scaletofit) can utilize so:
// here previewrect rectangle holds camera's preview size, // picturerect , nativeresrect hold camera's image size , // native resolution, respectively. rectf previewrect = new rectf(0, 0, 480, 800), picturerect = new rectf(0, 0, 1080, 1920), nativeresrect = new rectf(0, 0, 1952, 2592), resultrect = new rectf(0, 0, 480, 800); final matrix scalematrix = new matrix(); // create matrix scales coordinates of preview size rectangle // camera's native resolution. scalematrix.setrecttorect(previewrect, nativeresrect, matrix.scaletofit.center); // map result rectangle new coordinates scalematrix.maprect(resultrect); // create matrix scales coordinates of image size rectangle // camera's native resolution. scalematrix.setrecttorect(picturerect, nativeresrect, matrix.scaletofit.center); // invert it, matrix downscales rectangle // native resolution actual image size scalematrix.invert(scalematrix); // , map result rectangle coordinates in image size rectangle scalematrix.maprect(resultrect);
after these manipulations resultrect
hold coordinates of area within image taken photographic camera correspond same image you've seen in preview of app. can cutting area image bitmapregiondecoder.html#decoderegion(android.graphics.rect, android.graphics.bitmapfactory.options) method.
and that's it.
android android-camera aspect-ratio
Comments
Post a Comment