Android Camera in portrait error for Samsung Galaxy - android

How to keep camera in portrait mode in all devices?
my app is above 2.2
I tried
public void setRotation(int rotation) {
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;
CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (cameraInfo.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (cameraInfo.orientation - degrees + 360) % 360;
}
mCamera.stopPreview();
mCamera.setDisplayOrientation(result);
mCamera.startPreview();
}
but samsung devices ignoring mCamera.setDisplayOrientation(90)
Edited:
devices I found ignoring this:
Galaxy y(GT-S5360)(Android 2.3.6)
Galaxy Ace(GT-S5830)(Android 2.6.3)
but Galaxy S2 and other higher phones working well.

You can try
public int getCorrectCameraOrientation(CameraInfo info, Camera camera) {
int rotation = context.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;
}else{
result = (info.orientation – degrees + 360) % 360;
}
return result;
}
Use this method as
mCamera.setDisplayOrientation(getCorrectCameraOrientation(mCameraInfo, mCamera));
This is not enough for rotation handling, you need to do below step also
mCamera.getParameters().setRotation(getCorrectCameraOrientation(mCameraInfo, mCamera));

Related

How to make custom camera which Capture horizontally always in Native Android?

I want to capture a video always horizontally.
If the user rotates the phone, I want to capture the video horizontally.
I have one reference for that but I want to make it for my own with customization.
how can I implement this in a smooth way?
Try this
public static void setCameraDisplayOrientation(int phoneRotation, int cameraId, Camera camera) {
Camera.CameraInfo info = new Camera.CameraInfo();
info = Camera.getCameraInfo(cameraId, info);
int degrees = 0;
switch (phoneRotation) {
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;
} else {
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
when phoneRotation is
int phoneRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
By the way this answer originally refactored from some answer in stackoverflow.

Status bar remains in landscape mode when using camera android

So after a while of messing with the camera, I've finally got the actual camera orientation working with this:
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);
}
However, the status bar (the thing at the top with time and network info) is still in landscape.
I've already included this:
android:screenOrientation="portrait"
in my AndroidManifest.xml
Any ideas on how to fix this?
If you know what you want to set your orientation as you can use setRequestedOrientation(int requestedOrientation) in your Activity

Rotating device changing image orientation ?

This is my camera Activity which is in Portrait mode. I am rotating the device and the camera is rotating with it.
This is my second Activity where my image is displaying on the ImageView upside down, received from PictureCallback(). I already tried ExifInterface but its returning orientation 0.
I had a similar Problem. Orientation 0 in ExifInterface means undefined. If that occurs i ask the MediaStore and in my case it always returns the correct orientation if ExifInterface returns 0.
String[] cols = { MediaStore.Images.Media.ORIENTATION };
Cursor cur = context.getContentResolver().query(uri, cols, null, null, null);
int orientation = 0;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(MediaStore.Images.Media.ORIENTATION));
}
Setting for camera:
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
try {
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);
} catch (Exception e) {
// TODO: handle exception
}
}
And use ExifInterface when display result

Force a camera to always open in portrait mode in android [duplicate]

This question already has answers here:
Android Camera in Portrait on SurfaceView
(6 answers)
Closed 7 years ago.
Is it possible to open the Camera Intent in android in only portrait mode, that is even if the user flipped the device, the camera will still be in portrait mode,
I tried the following answer: Force Portrait Mode but didn't work, Thanks
Try it -
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 void initCamera() {
if(mCamera == null) {
mCamera = Camera.open();
setCameraDisplayOrientation(activity, CameraInfo.CAMERA_FACING_BACK, mCamera);
mCamera.unlock();
}
}

Android camera rotate

I have a Motorola Defy OS Android 2.1 and I make an application with camera Preview. The problem is that the camera works fine on Samsung Galaxy S with Android 2.1, but on Motorola the camera is rotated with 90 degrees. I have tried to do this:
Parameters parameters = camera.getParameters();
parameters.setRotation(90);
but it's not working. I didn't find any solution yet.
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
camera.setDisplayOrientation(90);
lp.height = previewSurfaceHeight;
lp.width = (int) (previewSurfaceHeight / aspect);
} else {
camera.setDisplayOrientation(0);
lp.width = previewSurfaceWidth;
lp.height = (int) (previewSurfaceWidth / aspect);
}
There is official example code for this in the Android docs now (under setDisplayOrientation()):
public static 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);
}
camera.setDisplayOrientation(int) is not exist under 2.1!
And this code may work, but fail in my milestone/droid :(
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);
you can see more in http://code.google.com/p/android/issues/detail?id=1193#c42
I found this code that works in Android 1.6 and over (works for me using 2.1 and present preview in portrait mode without rotating)
public void surfaceCreated(SurfaceHolder holder){
try{
camera = Camera.open();
setDisplayOrientation(camera, 90);
camera.setPreviewDisplay(holder);
camera.startPreview();
}catch(IOException e){
Log.d("CAMERA", e.getMessage());
}
}
protected void setDisplayOrientation(Camera camera, int angle){
Method downPolymorphic;
try
{
downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(camera, new Object[] { angle });
}
catch (Exception e1)
{
}
}
The activity has android:screenOrientation="portrait" on AndroidManifest.xml
http://code.google.com/p/android/issues/detail?id=1193#c42
public static 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);
}
I think that you can not make any setting to support the API 2.2 to 2.1. The API doesn't have in your current device lib. You must change to 2.2 to support API level 8. By the way, I also try to use the API level 7:
Parameters parameters = camera.getParameters();
parameters.setRotation(90);
This function works well on the Samsung Galaxy Tab but Nexus one. The Samsung Galaxy Tab uses OS 2.2.0 and the Nexus one uses OS 2.2.1. When I try to use API level 8:
camera.setDisplayOrientation(90);
both of them work well. So I think that the API level 7 has problem when we use on Android OS 2.2.1.

Categories

Resources