Center Crop Image In Proper Size To Set On Imageview
I am using camera API to take picture i have to open camera in different sizes according to my Image view size. I am following the sample project which we get inside Android sdk/sa
Solution 1:
@Akanksha Please use this below code, you just need to pass the path of the saved image, and the hight and width of our imageview. This code works perfectly for me.
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
publicclassImageHandler {
/**
* Decode and sample down a bitmap from a file to the requested width and
* height.
*
* @param filename
* The full path of the file to decode
* @param reqWidth
* The requested width of the resulting bitmap
* @param reqHeight
* The requested height of the resulting bitmap
* @return A bitmap sampled down from the original with the same aspect
* ratio and dimensions that are equal to or greater than the
* requested width and height
*/publicstatic Bitmap decodeSampledBitmapFromFile(String filename,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensionsfinal BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filename, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filename, options);
}
publicstaticintcalculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of imagefinalintheight= options.outHeight;
finalintwidth= options.outWidth;
intinSampleSize=1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
// This offers some additional logic in case the image has a// strange// aspect ratio. For example, a panorama may have a much larger// width than height. In these cases the total pixels might// still// end up being too large to fit comfortably in memory, so we// should// be more aggressive with sample down the image (=larger// inSampleSize).finalfloattotalPixels= width * height;
// Anything more than 2x the requested pixels we'll sample down// further.finalfloattotalReqPixelsCap= reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
}
return inSampleSize;
}
}
I call this method inside async task because it may take too much UImemory and time Here is how I call it.
classAsyncingextendsAsyncTask {
privateint reqWidth;
privateint reqHeight;
private ImageView iv;
private String fileName;
private ProgressDialog pd;
publicAsyncing(int reqWidth, int reqHeight, ImageView iv,
String fileName) {
super();
this.reqWidth = reqWidth;
this.reqHeight = reqHeight;
this.fileName = fileName;
this.iv = iv;
}
@Overrideprotected Bitmap doInBackground(String... params) {
return ImageHandler.decodeSampledBitmapFromFile(params[0],
reqWidth, reqHeight);
}
@OverrideprotectedvoidonPostExecute(Bitmap result) {
iv.setImageBitmap(result);
if (pd.isShowing()) {
pd.setMessage(getString(R.string.completed));
pd.dismiss();
}
super.onPostExecute(result);
}
@OverrideprotectedvoidonProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@OverrideprotectedvoidonPreExecute() {
pd = ProgressDialog.show(CustomerDetailsActivity.this, "",
getString(R.string.processing_signature));
super.onPreExecute();
}
}
This is how you need to call the asynctask
signedImagePath = data.getExtras().getString("imagePath");
new Asyncing(signature_img.getWidth(), signature_img.getHeight(),
signature_img, "spenTest.png").execute(signedImagePath);
above code is written according to my requirements,you modify it according to yours.
Solution 2:
Center crop an image may be help you this.
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
intsourceWidth= source.getWidth();
intsourceHeight= source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.// To cover the final image, the final scaling will be the bigger// of these two.floatxScale= (float) newWidth / sourceWidth;
floatyScale= (float) newHeight / sourceHeight;
floatscale= Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaledfloatscaledWidth= scale * sourceWidth;
floatscaledHeight= scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap// should be centered in the new size give by the parametersfloatleft= (newWidth - scaledWidth) / 2;
floattop= (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now// beRectFtargetRect=newRectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,// scaled bitmap onto it.Bitmapdest= Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvascanvas=newCanvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
Post a Comment for "Center Crop Image In Proper Size To Set On Imageview"