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)、画像が大きすぎることを意味していると思います。

撮影したカメラ画像の代わりに小さな画像をコードに挿入してテストしましたが、これはカメラ画像のサイズであるという問題にもつながります。

だから私はThis From 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 を使用して写真を引き継いで新しいアクティビティに表示するアプリがあります。

Intentほとんどの場合、エクストラを使用してアクティビティ間でデータを渡すことは優れたソリューションです。ただし、ビットマップが大きいと、メモリ消費が原因で問題が発生します。これは、静的データ メンバーを慎重に使用する 1 つのケースです。

logcat で java.lang.OutOfMemoryError: bitmap size exceeded VM Budget(Heap Size=7431KB, Allocated=2956KB, Bitmap Size=19764KB) を取得しています。これは、イメージが大きすぎることを意味すると思います。

これは、何をしようとしても十分なメモリがないことを意味します。

コードを見ると、最小の解像度/サイズが検索され、それらの設定を使用してカメラが取得されると思います。

はい。

もしそうなら、どうすればそれを自分のコードに実装できますか?

通常、コピーと貼り付けは機能します。:-)

撮影する写真のサイズをカメラに伝えるには、 on を使用setPictureSize()Camera.Parametersます。ただし、サポートするサイズを選択する必要があり、すべてのデバイスが任意のサイズをサポートするわけではありません。getSupportedPictureSizes()どのサイズがサポートされているかは、電話するとわかります。これらのサイズのどれを使用するかを決定するのはあなた次第です。他の質問からリンクした例では、これらのサイズを繰り返し処理し、面積の点で最小のものを見つけます。

そうは言っても、画像サイズに関する唯一の問題がアクティビティからアクティビティへの受け渡しである場合は、最初に静的データ メンバー ルートを使用します。null画像が不要になったら、必ずその静的データ メンバーを削除してください。

もしかしてこういうこと?

繰り返しますが、それはあなたの目的が何であるかによって異なります。問題を 1 つしか明確にしていないのに、解決策を探し回っているように見えます (さらに、メモリ不足エラーが発生している場所を教えてくれませんでした)。

フル解像度の写真を撮り、それをファイルに保存し、メモリにサムネイルを保持することが目的の場合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 に答える