返回相机图像时失败粘合剂交​​易粘合剂、图像、相机

2023-09-12 10:17:27 作者:花落尽执何手

我带回了从摄像头的意图回父意图的相机拍摄的图像(如一个byte [使用putExtra])时,你得到的logcat中的失败粘合剂事务错误。我不明白为什么,它不喜欢它的一个大的位图或任何东西。它只是发生在我拍照光线充足,因为这时的byte []越大。离开摄像机的意图时出现的错误。有没有人看到我的code错了吗?

下面是相机意图code:

 包example.imaging.ape;

进口java.io.IOException异常;
进口java.util.Iterator的;
进口java.util.Set中;

进口android.app.Activity;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.PixelFormat;
进口android.hardware.Camera;
进口android.hardware.Camera.AutoFocusCallback;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.MotionEvent;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
进口android.view.View;
进口android.view.Window;
进口android.view.WindowManager;
进口android.view.View.OnTouchListener;

公共类TakePicture扩展活动实现SurfaceHolder.Callback {
     相机mCamera;
     布尔米previewRunning = FALSE;
     INT imageLayoutHeight;
     INT imageLayoutWidth;

     @覆盖
     公共无效的onCreate(包savedInstanceState){
          super.onCreate(savedInstanceState);

          //设置相机表面
          。getWindow()和setFormat(PixelFormat.TRANSLUCENT);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow()setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN)。
          的setContentView(R.layout.cameralayout);

          SurfaceView mSurfaceView =(SurfaceView)findViewById(R.id.hist_surface_camera);
          SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder();
          mSurfaceHolder.addCallback(本);
          mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
          捆绑额外= getIntent()getExtras()。
          imageLayoutHeight = extras.getInt(layoutHeight);
          imageLayoutWidth = extras.getInt(layoutWidth);

          OnTouchListener touchListener =新View.OnTouchListener(){
               公共布尔onTouch(视图V,MotionEvent E){

                    的System.out.println(使图像);
                    mCamera.autoFocus(CB);
                    返回false;
               }
          };

          //设置触摸监听器
          mSurfaceView.setOnTouchListener(touchListener);

     }

     AutoFocusCallback CB =新AutoFocusCallback(){
          公共无效onAutoFocus(布尔成功,相机C){
               c.takePicture(NULL,NULL,mPictureCallback);
          }
     };

     Camera.PictureCallback mPictureCallback =新Camera.PictureCallback(){
          公共无效onPictureTaken(byte []的为imageData,摄像机C){
               的System.out.println(拍照,现在回到);
               意图resultIntent =新意图();
               resultIntent.putExtra(cameraImage,为imageData);
               的System.out.println(放额外);
               的setResult(Activity.RESULT_OK,resultIntent);
               完();
          }
     };

     //初始化相机
     公共无效surfaceCreated(SurfaceHolder持有者){
          mCamera = Camera.open();
     }

     公共无效surfaceChanged(SurfaceHolder持有人,INT格式,INT W,INT高){
          如果(M previewRunning){
               mCamera.stop preVIEW();
          }

          Camera.Parameters p值= mCamera.getParameters();

          P.SET previewSize(H,W);
          的System.out.println(previewSize:+ H +,+ w)的;
          p.setPictureSize(H * 3,W * 3); //大约是1200x900
          P.SET(旋转,90);
          mCamera.setParameters(对);

          尝试 {
               mCamera.set previewDisplay(保持器);
          }赶上(IOException异常E){
               e.printStackTrace();
          }

          mCamera.start preVIEW();
          米previewRunning = TRUE;
     }

     公共无效surfaceDestroyed(SurfaceHolder持有者){
          mCamera.stop preVIEW();
          米previewRunning = FALSE;
          mCamera.release();
     }

}
 

这里是code调用摄像头意图:

 意向意图=新的意图(Example.this,TakePicture.class);
intent.putExtra(layoutWidth,layoutWidth);
intent.putExtra(layoutHeight,layoutHeight);
startActivityForResult(意向,0);
 

解决方案

由于某些原因,Android不,当您尝试通过原始byte []数组或者从它创建了一个位图喜欢。有些人已经成功COM pressing生成位图,并通过它通过意向。我会建议先保存图像文件和发送它通过意向的路径。

I get the Failed binder transaction error in the logcat when returning the image taken with the camera from the camera intent back to the parent intent (as a byte[] using putExtra). I don't understand why, its not like its a big bitmap or anything. It only happens when i take pictures with lots of light, because then the byte[] is bigger. The error occurs when leaving the camera intent. Does anyone see a mistake in my code?

Here is the code of the camera intent:

package example.imaging.ape;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;

public class TakePicture extends Activity implements SurfaceHolder.Callback{
     Camera mCamera;
     Boolean mPreviewRunning = false;
     int imageLayoutHeight;
     int imageLayoutWidth;

     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          //setup camera surface
          getWindow().setFormat(PixelFormat.TRANSLUCENT);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
          setContentView(R.layout.cameralayout);

          SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.hist_surface_camera);
          SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder();
          mSurfaceHolder.addCallback(this);
          mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
          Bundle extras = getIntent().getExtras();
          imageLayoutHeight = extras.getInt("layoutHeight");
          imageLayoutWidth = extras.getInt("layoutWidth");

          OnTouchListener touchListener = new View.OnTouchListener() {
               public boolean onTouch(View v, MotionEvent e) {

                    System.out.println("MAKING PICTURE");
                    mCamera.autoFocus(cb);             
                    return false;
               }
          };

          //setup touch listener
          mSurfaceView.setOnTouchListener(touchListener);

     }

     AutoFocusCallback cb = new AutoFocusCallback() {
          public void onAutoFocus(boolean success, Camera c) {
               c.takePicture(null, null, mPictureCallback);
          }   
     };

     Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
          public void onPictureTaken(byte[] imageData, Camera c) {
               System.out.println("Picture taken, now returning");              
               Intent resultIntent = new Intent();
               resultIntent.putExtra("cameraImage", imageData);
               System.out.println("put Extra");
               setResult(Activity.RESULT_OK, resultIntent);
               finish();           
          }
     };

     //initialize camera
     public void surfaceCreated(SurfaceHolder holder) {
          mCamera = Camera.open();
     }

     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
          if (mPreviewRunning) {
               mCamera.stopPreview();
          }

          Camera.Parameters p = mCamera.getParameters();

          p.setPreviewSize(h, w);
          System.out.println("PreviewSize: " + h + "," + w);
          p.setPictureSize(h*3,w*3); // is around 1200x900
          p.set("rotation", 90);
          mCamera.setParameters(p);

          try {
               mCamera.setPreviewDisplay(holder);
          } catch (IOException e) {
               e.printStackTrace();
          }

          mCamera.startPreview();
          mPreviewRunning = true;
     }

     public void surfaceDestroyed(SurfaceHolder holder) {
          mCamera.stopPreview();
          mPreviewRunning = false;
          mCamera.release();
     }

}

And here is the code that calls the camera intent:

Intent intent = new Intent(Example.this, TakePicture.class);
intent.putExtra("layoutWidth",layoutWidth);
intent.putExtra("layoutHeight",layoutHeight);                   
startActivityForResult(intent,0);

解决方案

For some reason, Android doesn't like when you try to pass the raw byte[] array or a Bitmap created from it. Some people have had success compressing the resulting Bitmap and passing that via Intent. I would recommend saving the image to a file first and sending it's path via the Intent.