简体   繁体   English

Android:使用自定义相机活动拍照并返回

[英]Android: take photo with custom camera activity and return it

I'm using a custom camera class in android. 我在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. 我正在使用一个Intent返回图片。

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) 我已经测试了两种方法,但一种方式是在拍照后相机被挡住(无例外),而另一种方式是,在活动结果中,我无法拍摄到我拥有的picutre(位图或字节数组)放入Intent(因为它为null)

Here 2 classes, MainActivity and GGCameraActivity (the activity that runs the camera and takes the photo). 这里有2类,MainActivity和GGCameraActivity(运行相机并拍照的活动)。

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). 我没有作为异常的错误(我的意思是,应用程序不会停止导致异常的原因),但是我已经多次调试了类,并且我总是认为引发了异常,但是在某些地方(但不是我)捕获了异常“ Camera.class”(android提供的相机类)。 I think this because I always enter in a code snippet (in Camera.class) that's to throw an exception. 我想这是因为我总是输入一个引发异常的代码段(在Camera.class中)。 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. 此代码段位于Camera.class中,我总是输入该代码段,但是,如果我不调试该应用程序,则只需运行它(不从MainActivity拍摄捕获的照片)即可正常运行,该应用程序不会崩溃。

EDIT 1: I need a custom camera activity, Intent(MediaStore.ACTION_IMAGE_CAPTURE); 编辑1:我需要自定义相机活动, Intent(MediaStore.ACTION_IMAGE_CAPTURE); is not the thing I need. 这不是我需要的东西。

EDIT 2: I have tested to return a simple integer. 编辑2:我已经测试过返回一个简单的整数。 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. 调试我可以再次看到上面提到的IllegalArgumentException(),但是应用程序不会崩溃。 Here the code (changes only in callback and onActivityResult for tke integer instead of byte[]): 此处的代码(仅在tke整数而不是byte []的callback和onActivityResult中更改):

Callback of takePicture: 回调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 MainActivity中的onActivityResult

[...]

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. 编辑3:在调试时,我已进入完成方法。 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. 我已经读到Intent对于处理大型对象不是很好。 I have tested to put a byte[] of 20 components only and there is no problem. 我已经测试过只将byte []包含20个组件,并且没有问题。 But, with the image byte[] (400k size more or less) the app keeps blocked. 但是,使用图片字节[](或多或少400k大小),该应用会一直处于阻止状态。 I have read this things fast and I'm not sure if it's correct 100%. 我已快速阅读此书,但不确定100%是否正确。

I have read too that for sharing large objects between activities the best idea is to use static variables (or Parcelables maybe?). 我也读过,为了在活动之间共享大型对象,最好的主意是使用静态变量(或者说Parcelables?)。

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. 我想我在调试器中看到的异常(但未抛出)是由于在Intent中放置了一个太大的数组并将其作为结果( setResult() )返回的事实而产生的,但是我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何拍摄照片,保存并正确传递Android自定义相机的照片uri? - How to take a photo, save it and pass the photo uri for Android custom camera properly? 使用android相机拍照后,将用户带回之前的活动 - take user back to previous activity after taking photo using android camera Android Studio-Java如何拍照(相机) - Android Studio - Java how to take photo(camera) 用相机拍摄照片三星Galaxy S5销毁活动 - Take Photo with Camera Samsung galaxy s5 destroy activity Android使用Android前置摄像头拍照 - android take photo using android front camera not working Android后置摄像头无需预览即可拍照 - Android Rear Facing Camera Take Photo without preview 在 android 中使用相机拍照后如何永久保存图像? - How to save image PERMANENTLY after using camera to take photo in android? 用相机拍照并保存在 FileProvider 中 - Take photo with Camera and save in FileProvider 当我旋转相机应用程序中的屏幕并且我的主要活动失败时,Android Camera无法将照片返回主要活动 - Android Camera not returning photo to main activity when I rotate the screen in the camera application and my main activity fails ANDROID STUDIO:使用Camera API拍照->将此图片发送到另一个活动 - ANDROID STUDIO: Take picture with Camera API -> Send this picture to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM