Android减小相机图片大小

6

更新:在新的活动中更改位图大小可能会解决问题。

String myRef = this.getIntent().getStringExtra("filepath");
    File imgFile = new  File(myRef);

    Log.e("BeatEmUp", myRef);
    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        ImageView myImage = (ImageView) findViewById(R.id.imagepunch);
        myImage.setImageBitmap(myBitmap);

    }

可能像这样?

Bitmap scaled = Bitmap.createScaledBitmap(bit, 200, 200, true);

或者以下的方式可能更好

到目前为止,我的应用程序拍照并使用 Intent 将图片传送过来并在新的 Activity 中显示。一旦我完成了这个步骤,我有一些代码可以使用 onClick 显示在顶部的图像。问题是,当照片被拍摄时,它以我认为的最大尺寸拍摄,所以我的应用程序强制关闭。

在我的日志输出中,我得到了 java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7431KB, Allocated=2956KB, Bitmap Size=19764KB) 这意味着图像太大了。

我已经测试了将一张较小的图像放入代码中,而不是使用摄像机拍摄的图片,它也能工作,这也让我回到了它是相机图片尺寸的问题。

因此,我正在尝试实现 CommonsWare 的这个示例,但到目前为止没有成功。通过查看代码,我认为它搜索最小的分辨率/尺寸,然后使用这些设置拍摄照片,我的想法是正确的,请问我该如何将其实现到我的代码中呢?

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

final int RESULT_SAVEIMAGE = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl 
        = new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);

    Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
    buttonTakePicture.setOnClickListener(new Button.OnClickListener(){

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            camera.takePicture(myShutterCallback, 
                    myPictureCallback_RAW, myPictureCallback_JPG);



        }});

}

ShutterCallback myShutterCallback = new ShutterCallback(){

    public void onShutter() {
        // TODO Auto-generated method stub

    }};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub

    }};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        /*Bitmap bitmapPicture 
            = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */
        int imageNum = 0;
        Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
        imagesFolder.mkdirs(); // <----
        String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        File output = new File(imagesFolder, fileName);
        while (output.exists()){
            imageNum++;
            fileName = "image_" + String.valueOf(imageNum) + ".jpg";
            output = new File(imagesFolder, fileName);
        }

        Uri uriSavedImage = Uri.fromFile(output);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);


        OutputStream imageFileOS;
        try {
            imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(AndroidCamera.this, 
                    "Image saved", 
                    Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Intent intent = new Intent(getBaseContext(), Punch.class);
        intent.putExtra("filepath",Uri.parse(output.getAbsolutePath()).toString());
        //just using a request code of zero
        int request=0;
        startActivityForResult(intent,request); 
    }};

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if(previewing){
        camera.stopPreview();
        previewing = false;
    }

    if (camera != null){
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            camera.release();
            e.printStackTrace();
        }
    }
}





public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub

    camera = Camera.open();
    try {
           Camera.Parameters parameters = camera.getParameters();

           if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
              // This is an undocumented although widely known feature
              parameters.set("orientation", "portrait");
              // For Android 2.2 and above
              camera.setDisplayOrientation(90);
              // Uncomment for Android 2.0 and above
              parameters.setRotation(90);
           } else {
              // This is an undocumented although widely known feature
              parameters.set("orientation", "landscape");
              // For Android 2.2 and above
              camera.setDisplayOrientation(0);
              // Uncomment for Android 2.0 and above
              parameters.setRotation(0);
           }


          camera.setParameters(parameters);
          camera.setPreviewDisplay(holder);
      } catch (IOException exception) {
         camera.release();


       }
        camera.startPreview();

    }


public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    if(previewing && camera != null) {
        if(camera!=null) {
            camera.stopPreview();
            camera.release();  
            camera = null;
        }
        previewing = false;
    }
}


}

你想做的是 --- 拍照,然后在点击图片时将图像传递到新活动中?如果我没错的话,你需要一个中等大小的图像来点击? - Code_Life
@Mohit Sharma:我不太明白你的意思,但是是的,就像那样。 - Matt
2个回答

13
到目前为止,我的应用程序可以拍照,然后使用Intent将图片传递并在新Activity中显示。在大多数情况下,使用Intent extras传递活动之间的数据是一个很好的解决方案。然而,由于内存消耗问题,大位图会导致问题。这是我会小心地使用静态数据成员的一种情况。在我的logcat中,我得到了java.lang.OutOfMemoryError:bitmap size exceeds VM budget(Heap Size=7431KB, Allocated=2956KB, Bitmap Size=19764KB)的错误,我认为这意味着图像太大了。这意味着您没有足够的内存来完成您正在尝试的操作。查看代码,我认为它搜索最小的分辨率/大小,然后使用这些设置使用相机,我是否正确?是的。如果是这样,我该如何将其实现到我的代码中?通常复制和粘贴会起作用。 :-)
要告诉相机拍摄什么尺寸的照片,请在Camera.Parameters上使用setPictureSize()。但是,您必须选择它支持的尺寸,而不是所有设备都支持任意大小。调用getSupportedPictureSizes()将告诉您支持哪些尺寸。由您确定要使用哪些尺寸。在我链接的示例中,我遍历这些尺寸并找到面积最小的尺寸。
话虽如此,如果图像大小的唯一问题是从活动传递到活动,我会首先使用静态数据成员路线。只需确保在不再需要图像时将其静态数据成员设置为null即可。

也许像这样?

同样,这取决于您的目标是什么。您似乎正在寻找解决方案,而只表达了一个问题(甚至在那里,您也没有告诉我们出现内存不足错误的位置)。

如果你的目标是拍摄全分辨率的照片,保存在文件中,然后在内存中保留缩略图,createScaledBitmap() 是一个很好的解决方案。
如果你的目标是首先拍摄缩略图大小的照片(即,你不需要或不想要全分辨率的图像),使用 setPictureSize()
如果你的目标是始终使用全分辨率的图片,请仔细使用静态数据成员来消除任何不必要的 Bitmap 副本,并查看是否足够。这取决于你正在尝试使用图像做什么,可能会不够用。

非常棒的回答,我明白你的意思了。我的目标基本上是一旦相机拍摄的照片被带到下一个活动中,它就会有一个onClick,每次点击都会在相机照片上叠加不同的图像。这就是当我尝试onCLick时出现“内存错误”导致应用程序崩溃的原因。 - Matt
你的链接中的代码是否会自动将相机设置为根据手机拍摄最低尺寸,还是用户必须手动选择? - Matt
@Matt:在我的示例中,用户没有选择的权利。如果您希望用户选择大小,则应使用getSupportedPictureSizes()来填充SpinnerAlertDialog或类似的内容。 - CommonsWare
我只想让它以手机允许的最小值来进行。尝试将您的代码实现到此处http://pastebin.com/rx0vDmmN中。 - Matt
@Matt:在 surfaceChanged() 方法中,获取你的 Camera.Parameters 并按照你的需求进行配置。 - CommonsWare

2

尝试从相机捕获图像。

Intent cameraActivity=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraActivity.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(left));
            startActivityForResult(cameraActivity,CAMERA_PIC_REQUEST_LEFTIMAGE);

这将返回您大尺寸的图像,现在要将其缩小,您可以使用类似以下的内容

    BitmapFactory.Options options=new BitmapFactory.Options();
                        options.inSampleSize = 6;
                        FileInputStream fileInputStream;    
                        try {
                                fileInputStream=new FileInputStream(left);
                                leftPhoto=BitmapFactory.decodeStream(fileInputStream,null,options);
                                leftImage.setImageBitmap(leftPhoto);
                                fileInputStream.close();
                            } catch (FileNotFoundException e) {
                                Toast.makeText(getCurrentContext(), e.getMessage(),Toast.LENGTH_LONG).show();
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                                Toast.makeText(getCurrentContext(), e.getMessage(),Toast.LENGTH_LONG).show();
                            }

现在您可以在此添加“点击”功能。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接