In Android development, if the picture is cropped after taking pictures, if it is called the system's cropping, as follows:
/* * Cropped pictures */ private void cropPhoto() { Intent intent = new Intent(""); Uri uri = ("file://" + picSavePath); (uri, "image/*"); ("crop", "true"); // ("aspectX", 3); // ("aspectY", 2); ("outputX", cropX); ("outputY", cropY); ("scale", "true"); (MediaStore.EXTRA_OUTPUT, uri); ("return-data", "false"); ("outputFormat", ()); ("noFaceDetection", "true"); // no face detection startActivityForResult(intent, CROP_PICTURE); }
In this way, the image is started to be cropped, but there will be a problem. When the image selected in the crop box is different from the shape of the entered cropX and xropY, for example, the passed parameter value is a rectangle of w>h, and the selection box selects a rectangle of w<h, this will cause the cropped image to be compressed and deformed.
In order to solve the problem of compression deformation, my idea is as follows:
1. Crop the image first, and do not set the default crop image size.
2. Scale the cropped pictures. Scaling is the scaling method of taking angle matrix
The code is as follows:
1.
/* * Cropped pictures, */ private void cropPhotoAndZoom() { Intent intent = new Intent(""); Uri uri = ("file://" + picSavePath); (uri, "image/*"); ("crop", "true"); ("scale", "true"); (MediaStore.EXTRA_OUTPUT, uri); ("return-data", "false"); ("outputFormat", ()); ("noFaceDetection", "true"); // no face detection startActivityForResult(intent, CROP_PICTURE_ANDZOOM); }
2.
/** * After cropping, scale according to the aspect ratio of the crop box and the size according to the needs of the picture. * * @param path * @param x * Original required size width * @param y * heiht * @return */ public static Bitmap toBigZoom(String path, float x, float y) { ("bitmaputil", "path---" + path + "--x--y--" + x + "--" + y); Bitmap bitmap = (path); if (bitmap != null) { int w = (); int h = (); float sx = 0; float sy = 0; if ((float) w / h >= 1) { sx = (float) y / w; sy = (float) x / h; ("bitmaputil---", "w/h--->=1"); } else { sx = (float) x / w; sy = (float) y / h; ("bitmaputil---", "w/h---<1"); } Matrix matrix = new Matrix(); (sx, sy); // The ratio of length and width enlargement and reduction Bitmap resizeBmp = (bitmap, 0, 0, w, h, matrix, true); ("bitmaputil---", "w---" + () + "h--" + ()); return resizeBmp; } return null; }
In the code in 2, the w and h ratio of the crop box is determined by determining whether the picture is enlarged horizontally or vertically. The effect after magnification can basically meet the needs.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.