我正在尝试缩放用相机拍摄的照片,但我不确定在哪里或如何执行此操作。现在代码正在访问相机,拍照并将其显示在 ListView 中,我也想获取图片路径,但我也不确定如何执行此操作。任何帮助将不胜感激。
/**
* This function is called when the add player picture button is clicked.
* It accesses the devices gallery and the user can choose a picture
* from the gallery.
* Or if the user chooses to take a picture with the camera, it handles that
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
this.picPath = selectedImage.getPath();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView = (ImageView) findViewById(R.id.imagePlayer);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
谢谢
最佳答案
获取位图图像后,您可以使用 Bitmap 类中的 createScaledBitmap 静态方法
Bitmap.createScaledBitmap(yourBitmap, 50, 50, true); // Width and Height in pixel e.g. 50
但在未来极端内存不足的情况下...... 如果您不小心,位图会迅速消耗您的可用内存预算,导致应用程序因可怕的异常而崩溃: java.lang.OutofMemoryError:位图大小超出 VM 预算。
所以为了避免 java.lang.OutOfMemory 异常,请在解码之前检查位图的尺寸,除非您绝对相信来源会为您提供大小可预测且适合可用空间的图像数据内存。
// below 3 line of code will come instead of
//imageView.setImageBitmap(bitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG,100,stream);
imageView.setImageBitmap(decodeSampledBitmapFromByte(stream.toByteArray(),50,50));
BitmapFactory 类提供了几种解码方法(decodeByteArray()、decodeFile()、decodeResource() 等),用于从各种来源创建位图。根据您的图像数据源选择最合适的解码方法。这些方法试图为构造的位图分配内存,因此很容易导致 OutOfMemory 异常。每种类型的解码方法都有额外的签名,让您可以通过 BitmapFactory.Options 类指定解码选项。在解码时将 inJustDecodeBounds 属性设置为 true 可避免内存分配,为位图对象返回 null 但设置 outWidth、outHeight 和 outMimeType。此技术允许您在构建(和内存分配)位图之前读取图像数据的尺寸和类型。
// please define following two methods in your activity
public Bitmap decodeSampledBitmapFromByte(byte[] res,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(res, 0, res.length,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(res, 0, res.length,options);
}
public 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) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
请引用 Android Training 中任何位图相关的以下链接 java.lang.OutofMemoryError: 位图大小超出 VM 预算 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
关于android - 缩小相机拍摄的照片并获取图片路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351429/