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. 问题是当图像被拍摄时,它是以我认为的最大尺寸拍摄的,所以我的应用程序被强制关闭。

在我的 logcat 中,我得到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;
    }
}


}
4

2 回答 2

13

到目前为止,我有我的应用程序正在拍照,然后使用 Intent 将图片传递过来并显示在新的 Activity 中。

在大多数情况下,使用Intentextras 在活动之间传递数据是一个很好的解决方案。但是,由于内存消耗,大位图会导致问题。这是我会小心使用静态数据成员的一种情况。

在我的 logcat 中,我收到 java.lang.OutOfMemoryError: bitmap size超出 VM budget(Heap Size=7431KB, Allocated=2956KB, Bitmap Size=19764KB) 我认为这意味着图像太大。

这意味着你没有足够的内存来做你想做的事情。

浏览代码,我认为它会搜索最小的分辨率/尺寸,然后使用这些设置拍摄相机,我认为这是正确的吗

是的。

如果是这样,我怎样才能在我的代码中实现它?

复制和粘贴通常有效。:-)

要告诉相机拍摄什么尺寸的照片,请setPictureSize()使用Camera.Parameters。但是,您必须选择它支持的尺寸,并且并非所有设备都支持任意尺寸。来电getSupportedPictureSizes()会告诉您支持哪些尺寸。由您决定使用哪些尺寸。在我从您的另一个问题链接到的示例中,我遍历了这些尺寸并找到了面积最小的那个。

话虽这么说,如果图像大小的唯一问题是将它从一个活动传递到另一个活动,我会先走静态数据成员路线。null当您不再需要图像时,请确保删除该静态数据成员。

可能是这样的?

同样,这取决于您的目标是什么。当您只阐明一个问题时,您似乎在四处寻找解决方案(即使在那里,您也没有告诉我们您从哪里得到内存不足错误)。

如果您的目标是拍摄全分辨率图片,将其保存在文件中,然后在内存中保存缩略图,createScaledBitmap()这是一个很好的解决方案。

如果您的目标是首先拍摄缩略图(即您不需要或不想要全分辨率图像),请使用setPictureSize().

如果您的目标是始终使用全分辨率图片,请小心使用静态数据成员来消除任何不必要的副本,Bitmap看看是否足够。可能不是,这取决于您要对图像执行的操作。

于 2012-01-06T13:25:26.677 回答
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();
                            }

现在你可以点击这个添加。

于 2012-01-06T13:25:42.993 回答