简体   繁体   中英

Android: take photo with custom camera activity and return it

I'm using a custom camera class in android. I want to take a photo (only one), finish the activity and return this photo, It doesn't matter if i do this with a bitmap or a bytearray. I'm using an Intent to return the picture.

I have tested 2 ways for do it, but in one way the camera being blocked after take a photo (without exception) and in the other way, in activity result, I can't take the picutre (Bitmap or bytearray) that I have put in the Intent (because it's null)

Here 2 classes, MainActivity and GGCameraActivity (the activity that runs the camera and takes the photo).

MAIN ACTIVITY:

public class MainActivity extends ActionBarActivity{

private static final int CAMERA_ACTIVITY_ID = 98;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b1 = (Button)findViewById(R.id.b_empezar);
    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startButtonClick();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_configuracion) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int reqCode, int resCode, Intent handler){
    switch(reqCode){
    case CAMERA_ACTIVITY_ID:
        if(resCode == RESULT_OK){
            //get the byte array
            byte[] b = handler.getExtras().getByteArray(GGCameraActivity.PARAM_PHOTO);
            //'b' is null.
        }
        break;
    }
}

private void startButtonClick(){
    Intent i = new Intent(this, GGCameraActivity.class);
    startActivityForResult(i, CAMERA_ACTIVITY_ID);
}

}

CAMERA ACTIVITY:

public class GGCameraActivity extends Activity {

private Activity context;
private GGCameraPreview preview;
private Camera camera;
private ImageView fotoButton;


public static final String PARAM_PHOTO = "bmp";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ggcamera);

    context = this;

    fotoButton = (ImageView) findViewById(R.id.photo_button);
    fotoButton.setOnClickListener(photoButtonClick);

    preview = new GGCameraPreview(this,(SurfaceView) findViewById(R.id.ggcameraFragment));
    FrameLayout frame = (FrameLayout) findViewById(R.id.ggcameraPreview);
    frame.addView(preview);

    preview.setKeepScreenOn(true);
}

@Override
protected void onResume() {
    super.onResume();
    if (camera == null) {
        camera = Camera.open();
        camera.startPreview();
        camera.setErrorCallback(new ErrorCallback() {

            @Override
            public void onError(int error, Camera mcamera) {
                camera.release();
                camera = Camera.open();
                Log.d("Camera died", "error camera");
            }

        });
    }
    if (camera != null) {
        if (Build.VERSION.SDK_INT >= 14)
            setCameraDisplayOrientation(context,
                    CameraInfo.CAMERA_FACING_BACK, camera);
        preview.setCamera(camera);
    }
}

@Override
protected void onPause() {
    if (camera != null) {
        camera.stopPreview();
        preview.setCamera(null);
        camera.release();
        camera = null;
    }
    super.onPause();
}

private void setCameraDisplayOrientation(Activity activity, int cameraId,
        android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

private OnClickListener photoButtonClick = new OnClickListener() {
    @Override
    public void onClick(View v) {
        fotoButton.setClickable(false);
        camera.autoFocus(mAutoFocusCallback);
    }
};

Camera.AutoFocusCallback mAutoFocusCallback = new Camera.AutoFocusCallback() {
    @Override
    public void onAutoFocus(boolean success, Camera camera) {
        camera.takePicture(null, null, jpegCallback);
    }
};

private PictureCallback jpegCallback = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Intent resultIntent = new Intent();
        resultIntent.putExtra(PARAM_PHOTO, data);
        context.setResult(RESULT_OK, resultIntent);
        context.finish();
    }
};

}

NOTE:

I have no errors as exceptions (I mean, the application don't stops cause of an exception), but I have debugged the classes many times and always I think an exception is raised but catched in some place (but not by me) in "Camera.class" (camera class provided by android). I think this because I always enter in a code snippet (in Camera.class) that's to throw an exception. Here this code snippet:

if (msgType!= CAMERA_MSG_PREVIEW_FRAME &&
    msgType != CAMERA_MSG_RAW_IMAGE){
    throw new IllegalArgumentException("Unsopported message type: "+ msgType);
}

This code snippet is in Camera.class, I always enter it but, if I don't debug the app, just run it (not taking the captured photo from MainActivity) all works well, the app does not crash.

EDIT 1: I need a custom camera activity, Intent(MediaStore.ACTION_IMAGE_CAPTURE); is not the thing I need.

EDIT 2: I have tested to return a simple integer. I have the same error, the camera blocks after push the button of take photo and never returns to the main activity. Debuggin I can see again the IllegalArgumentException() mentioned above, but app doesn't crash. Here the code (changes only in callback and onActivityResult for tke integer instead of byte[]):

Callback of takePicture:

private PictureCallback jpegCallback = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Intent resultIntent = new Intent();
        resultIntent.putExtra("int", 5);
        setResult(RESULT_OK, resultIntent);
        finish();
    }
};

onActivityResult in MainActivity

[...]

case CAMERA_ACTIVITY_ID:
    if(resCode == RESULT_OK){
        int n = handler.getExtras().getInt("int");
    }
    break;

[...]

EDIT 3: While debugging, I have step into finish method. I have found that this exception is raised:

throw new UnsupportedOperationException(
            "startNextMatchingActivity can only be called from a top-level activity");

But, again, the application does not crash.

I have found the problem. I have read that Intents are not very good for handle large objects. I have tested to put a byte[] of 20 components only and there is no problem. But, with the image byte[] (400k size more or less) the app keeps blocked. I have read this things fast and I'm not sure if it's correct 100%.

I have read too that for sharing large objects between activities the best idea is to use static variables (or Parcelables maybe?).

Then when I take the photo, I put it in class with a static varaible and I take it from other activities.

I suppose that the exceptions I was seeing in the debugger (but not thrown) were produced by the fact of put a too large array in the Intent and return it as result ( setResult() ), but I'm not sure at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM