Force a Camera to Always Open in Portrait Mode in Android

Force portrait mode in camera

None of the solutions work.There is nothing wrong with the layouts either.I got it to work by running on a higher version(API10 to API15).
Weird!!

How to set Android default camera app for portrait orientation only

how may I lock camera app only for portrait

You don't. You did not write the camera app, as there are hundreds, perhaps thousands, of camera apps. It is up to the camera app and the user to handle orientation.

Besides:

  • on many devices, you will have the same problem in portrait mode

  • your landscape photo also appears to be stretched, though not as drastically

But if I captured a photo from landscape the taken image would set in screen with stretched

You have one, and perhaps two, problems:

  1. You are not taking into account the orientation of the photo, and so you are loading a landscape photo as if it were portrait.

  2. Your ImageView, or perhaps its parent, is probably misconfigured. If you want to maintain the aspect ratio of the photo, and you want that ImageView to fill some area, then the ImageView needs to have that same aspect ratio.

If you really want to only take photos in portrait mode, you would need to use android.hardware.Camera yourself, rather than launching a third-party camera app, in addition to addressing the problems I mention above.

Android: Camera preview orientation in portrait mode

To force portrait orientation:

set android:screenOrientation="portrait" in your AndroidManifest.xml and call camera.setDisplayOrientation(90); before calling camera.startPreview();

I have not tested on the GS2 but it works on every phone I have tested on.

Note: setDisplayOrientation was added in API Level 8

Controlling the camera to take pictures in portrait doesn't rotate the final images

The problem is when I saved the image I didn't do well.

@Override
public void onPictureTaken(byte[] data, Camera camera) {

String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss").format( new Date( ));
output_file_name = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + timeStamp + ".jpeg";

File pictureFile = new File(output_file_name);
if (pictureFile.exists()) {
pictureFile.delete();
}

try {
FileOutputStream fos = new FileOutputStream(pictureFile);

Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);

ExifInterface exif=new ExifInterface(pictureFile.toString());

Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
realImage= rotate(realImage, 90);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
realImage= rotate(realImage, 270);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
realImage= rotate(realImage, 180);
} else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")){
realImage= rotate(realImage, 90);
}

boolean bo = realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);

fos.close();

((ImageView) findViewById(R.id.imageview)).setImageBitmap(realImage);

Log.d("Info", bo + "");

} catch (FileNotFoundException e) {
Log.d("Info", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("TAG", "Error accessing file: " + e.getMessage());
}
}

public static Bitmap rotate(Bitmap bitmap, int degree) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();

Matrix mtx = new Matrix();
// mtx.postRotate(degree);
mtx.setRotate(degree);

return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

how do i set android camera view port always landscape?

I believe it is pretty easy to achieve what your customer wants. So, you hold the device in Portrait mode, and you record video at resolution 1280x720. Obviously, it is portrait oriented: 720 is width, and 1280 is height (note that probably the encoded frames are still 1280 width and 720 height, but the video has a special rotation flag so that a compliant decoder will show the output at correct orientation).

Now, crop the video so that the result is 406x720 (yes, you loose a lot of pixels this way). But the result will have the desired landscape orientation. 406 is height, 720 is width.

Now to technical details: you can crop either live or after recording. In the latter case, you can run ffmpeg either on a server or on the device.

In the former case, you must receive the camera callbacks, crop the arriving data, and pass it to encoder. You can do it completely in Java, using the modern MediaCodec API. If you choose to write some native code, you can use ffmpeg libraries to perform the necessary operations.

You can even resize the resulting frames, such that video decoders will play the 406x720 originals at 1280x720 resolution. The quality will never be as the original (portrait) video.

Why I am speaking about the weird 406 height? To keep the standard aspect ratio. You may choose 540x720 if you can use 4:3 aspect ratio.



Related Topics



Leave a reply



Submit