处理应用程序内的相机或图库拍摄的大位图

时间:2014-10-04 08:42:20

标签: android image android-camera

任何人都可以帮助处理8MP摄像头拍摄的大位图。 我正面临错误"位图太大而无法上传到纹理"。 什么是压缩图像的最佳方式,因为它的宽高比不会降低。

提前致谢。

2 个答案:

答案 0 :(得分:1)

动态更改高度和宽度相对于图像比率并创建新的位图图像

 Bitmap bitmap = Bitmap.createScaledBitmap(capturedImage, width, height, true);

答案 1 :(得分:0)

尝试在清单

中添加android:largeHeap="true"
<application
    ...
    android:hardwareAccelerated="false"
    android:largeHeap="true" >

或尝试此方法

private Bitmap getBitmap(String path) {

 Uri uri = getImageUri(path);
InputStream in = null;
 try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = mContentResolver.openInputStream(uri);

// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();



int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > 
      IMAGE_MAX_SIZE) {
   scale++;
}
Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ", 
   orig-height: " + o.outHeight);

Bitmap b = null;
in = mContentResolver.openInputStream(uri);
if (scale > 1) {
    scale--;
    // scale to max possible inSampleSize that still yields an image
    // larger than target
    o = new BitmapFactory.Options();
    o.inSampleSize = scale;
    b = BitmapFactory.decodeStream(in, null, o);

    // resize to desired dimensions
    int height = b.getHeight();
    int width = b.getWidth();
    Log.d(TAG, "1th scale operation dimenions - width: " + width + ",
       height: " + height);

    double y = Math.sqrt(IMAGE_MAX_SIZE
            / (((double) width) / height));
    double x = (y / height) * width;

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, 
       (int) y, true);
    b.recycle();
    b = scaledBitmap;

    System.gc();
   } else {
    b = BitmapFactory.decodeStream(in);
  }
  in.close();

   Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + 
   b.getHeight());
  return b;
 } catch (IOException e) {
 Log.e(TAG, e.getMessage(),e);
 return null;
 }