实现两个相机API,以支持更高和更高的Android API级别

时间:2017-04-21 12:19:10

标签: android eclipse camera

如何通过界面创建实现两个摄像头API(摄像头和摄像头2)的流程,以支持更高和更高的API级别。我在我的应用程序中实现了android.hardware.Camera,适用于22和更旧的API,但我需要也支持更高的API水平。我看到了这一点 Android camera android.hardware.Camera deprecated但我并不完全明白如何在我的代码中实现一个包含所有2个摄像头的界面。像上面链接中的方法。我的代码是:

private void captureImage(){         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //可以发送标准Intent动作,让相机应用程序捕获图像并将其返回。

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);//Creating file URI to store image

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);//The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. 

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

/**
 * Here we store the file URL as it will be null after returning from camera
 * application
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // save file URL in bundle as it will be null on screen orientation
    // changes
    outState.putParcelable("file_uri", fileUri);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // get the file URL
    fileUri = savedInstanceState.getParcelable("file_uri");
}

/**
 * Receiving activity result method will be called after closing the camera
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // successfully captured the image
            // display it in image view
            previewCapturedImage();
        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    }}

/**
 * Display image from a path to ImageView
 */
private void previewCapturedImage() {
    try {            

        imgPreview.setVisibility(View.VISIBLE);

        // bitmap factory
        BitmapFactory.Options options = new BitmapFactory.Options();

        // down sizing image as it throws OutOfMemory Exception for larger images

        options.inSampleSize = 8;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);

        imgPreview.setImageBitmap(bitmap);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

/**
 * ------------ Helper Methods ---------------------- 
 * */

/**
 * Creating file URI to store image
 */
public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/**
 * returning image 
 */
private static File getOutputMediaFile(int type) {

    // External SD card location
    File mediaStorageDir = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist

    if (!mediaStorageDir.exists()) {

        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date(type));
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    }  else {
        return null;
    }

    return mediaFile;
}

0 个答案:

没有答案