繁体   English   中英

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

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

我在android中使用自定义相机类。 我想拍摄一张照片(仅一张),完成活动并返回这张照片,这与我是否使用位图或字节数组无关。 我正在使用一个Intent返回图片。

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

这里有2类,MainActivity和GGCameraActivity(运行相机并拍照的活动)。

主要活动:

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);
}

}

相机活动:

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();
    }
};

}

注意:

我没有作为异常的错误(我的意思是,应用程序不会停止导致异常的原因),但是我已经多次调试了类,并且我总是认为引发了异常,但是在某些地方(但不是我)捕获了异常“ Camera.class”(android提供的相机类)。 我想这是因为我总是输入一个引发异常的代码段(在Camera.class中)。 下面的代码片段:

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

此代码段位于Camera.class中,我总是输入该代码段,但是,如果我不调试该应用程序,则只需运行它(不从MainActivity拍摄捕获的照片)即可正常运行,该应用程序不会崩溃。

编辑1:我需要自定义相机活动, Intent(MediaStore.ACTION_IMAGE_CAPTURE); 这不是我需要的东西。

编辑2:我已经测试过返回一个简单的整数。 我也有同样的错误,按下拍照按钮后相机挡住,再也没有回到主要活动了。 调试我可以再次看到上面提到的IllegalArgumentException(),但是应用程序不会崩溃。 此处的代码(仅在tke整数而不是byte []的callback和onActivityResult中更改):

回调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();
    }
};

MainActivity中的onActivityResult

[...]

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

[...]

编辑3:在调试时,我已进入完成方法。 我发现引发了此异常:

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

但是,再次,该应用程序不会崩溃。

我发现了问题。 我已经读到Intent对于处理大型对象不是很好。 我已经测试过只将byte []包含20个组件,并且没有问题。 但是,使用图片字节[](或多或少400k大小),该应用会一直处于阻止状态。 我已快速阅读此书,但不确定100%是否正确。

我也读过,为了在活动之间共享大型对象,最好的主意是使用静态变量(或者说Parcelables?)。

然后,当我拍摄照片时,我将其放在静态可变类中,并从其他活动中获取。

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

暂无
暂无

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

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