Skip to content Skip to sidebar Skip to footer

Setting Camera Orientation To Portrait Not Working

I am using this code (given below) to take picture on button click, the picture is taken successfully but it is in landscape mode, I have set camera orientation to portrait using p

Solution 1:

I suggest you to use the default camera option in the mobile, instead of setting camera in surfaceview. Please find my code below.

privatevoidcallCamera() {

    Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAMERA_REQUEST);
}

private Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

privatestatic File getOutputMediaFile(int type) {

    FilemediaStorageDir=newFile(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            returnnull;
        }
    }
    Randomgen=newRandom();
    intn=10000;
    n = gen.nextInt(n);

    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = newFile(mediaStorageDir.getPath() + File.separator
                + "IMG_" + n + ".jpg");
    } else {
        returnnull;
    }
    return mediaFile;
}
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent        data) {
    IntentResultresult= IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
 if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {

            pickFromCamera();
        } elseif (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, Message.userCancel, Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(this, "Failed to Capture Image", Toast.LENGTH_SHORT).show();
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

privatevoidpickFromCamera() {
    Stringpath= fileUri.getPath();
     //Use this fileUri 
}

If you want to get the size of the captured image.

privatevoid getIMGSize(String path){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;

    Log.e("SizeofImage",String.valueOf(imageWidth)+" "+String.valueOf(imageHeight));

}

Post a Comment for "Setting Camera Orientation To Portrait Not Working"