Trong ứng dụng của tôi, tôi phải chụp ảnh từ máy ảnh hoặc nhập từ thư viện, hiển thị ảnh đó ở chế độ xem trong hoạt động. Mọi thứ đều ổn, tôi nhận được hình ảnh từ cả hai và có thể đặt nó trên imageview mà không có bất kỳ ngoại lệ nào. Nhưng đôi khi hình ảnh không được thu nhỏ đúng cách và được kéo dài theo chiều dọc hoặc với hướng thay đổi. Hãy giúp tôi ra.Ảnh chụp từ máy ảnh hoặc thư viện khi sử dụng trong imageview hướng của nó sẽ thay đổi và đôi khi được kéo dài theo chiều dọc trong Android
Đây là mã của tôi để giải mã hình ảnh giới thiệu từ tài liệu android chính thức:
public static Bitmap decodeSampledBitmapFromResource(File photoFile, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(new FileInputStream(photoFile), null,
options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(new FileInputStream(photoFile),
null, options);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/(float) reqHeight);
final int widthRatio = Math.round((float) width/(float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
tôi sẽ thử nó và cho bạn biết ... @ Abhishek – Prashant
ok kiểm tra xem nó và voteup nếu nó là hữu ích để u .. –
Cảm ơn Abhishek, nó hoạt động fine..I've kiểm tra mã của bạn trong ứng dụng của tôi. – Prashant