0

how to implement the flow of both 2 camera APIs(camera and camera2) through interface creation to support older and higher API levels.I implemented android.hardware.Camera in my app and works well for 22 and older APIs but i need to support higher API levels either.I saw this Android camera android.hardware.Camera deprecated but i do not exactly understand how to implement in my codes an interface which will contain all the 2 cameras' methods like in the link above. my codes are:

private void captureImage() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//Standard Intent action that can be sent to have the camera application capture an image and return it.

    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;
}
Community
  • 1
  • 1
sunche
  • 1
  • 1
  • 1
    "I implemented android.hardware.Camera in my app and works well for 22 and older APIs but i need to support higher API levels" -- first, `android.hardware.Camera` works on "higher API levels". Second, your code is not using `android.hardware.Camera`. It is using `ACTION_IMAGE_CAPTURE`. – CommonsWare Apr 21 '17 at 12:24
  • @CommonsWare but sir i declared in manifest this line android.hardware.camera, so you mean even the devices with higher levels like 25 ie Nougat will work as charm without android.hardware.camera2 declaration and implementation! – sunche Apr 21 '17 at 12:44

0 Answers0