Using Camera in Portrait Orientation - android

I'm trying to develop an app which uses the Camera. So far it's been working well, except that I'm unable to force the orientation to be "portrait". It seems to work well if I force all activities to "landscape", because the camera preview seems to fit in landscape.
Is there anyway to use the Camera in portrait mode?

Android devices v2.2 and above contain and API to rotate the display to portrait. Devices below 2.2 are landscape only. Your best bet is to detect if the device is 2.2 and rotate 90 degrees. Fall back on landscape for devices under 2.2. The good news is most Android devices are on 2.2 and above.
Check out my answer here for more info:
Camera is wrong unless keyboard is open

public void surfaceCreated(SurfaceHolder holder)
{
// The Surface has been created, acquire the camera and tell it where to draw.
mCamera = Camera.open();
Parameters params = mCamera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
{
params.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
}
try
{
mCamera.setPreviewDisplay(holder);
}
catch (IOException exception)
{
mCamera.release();
mCamera = null;
}
}

edit: I was in the midst of Adobe AIR for Android development when I answered this question, and looking back at it, I realize this question didn't pertain to Adobe AIR.
Adobe says:
On devices that can change the screen orientation, such as mobile phones, a Video object attached to the camera will only show upright video in a landscape-aspect orientation. Thus, mobile apps should use a landscape orientation when displaying video and should not auto-rotate.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Camera.html
If you do really want to use the camera in portrait mode, my suggestion is to rotate the video object.
Here's some sample code that rotates the video object (_video) by an angle in degrees (source was pulled from elsewhere on stackoverflow):
var matrix:Matrix = _video.transform.matrix;
var rect:Rectangle = _video.getBounds(this);
matrix.translate(- (rect.left + (rect.width/2)), - (rect.top + (rect.height/2)));
matrix.rotate((angle/180)*Math.PI);
matrix.translate(rect.left + (rect.width/2), rect.top + (rect.height/2));
_video.transform.matrix = matrix;

Related

SurfaceView camera not respecting setCameraDisplayOrientation on some devices

The camera on the surface view I implemented was rotated 90 degrees anti-clockwise at the start. I used the following code to make it erect.
if (activity.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
camera.setDisplayOrientation(90);
}
This is working fine on most of the devices but some still have the same issue.
I've also tried the code given on the android dev page but in vain.
How can this be rectified? Some of the devices still having this issue are Moto G5 and Honor 9N

Camera data bytes[] always in landscape mode?

I have created a camera app and using the data to do something .But the data is always in landscape mode .
I have tried this which is making the preview in portrait mode but the byte[] I am getting still gives me a landscape photo.
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
I know I can make it as a bitmap and rotate it 90 but I need only the data from onPictureTaken.
I also tried
param.setRotation(90);
Which is not working on some of the devices.
You are right, this is how camera API works (unlike iOS). There are some efficient methods that can rotate the pixels to portrait if you really need this, but they never come at 0 cost. So the best practice is to adjust your code that processes the arriving preview frames.

SurfaceView displays the noise at the camera image

I have some troubles with displaying camera viewfinder on Galaxy S2 (android 2.3) and LG P500 (android 2.3). I have some noise (interference) on SurfaceView. Nevertheless, result photo hasn't such defects.
Other devices (include devices with 2.3 and Galaxy S2 with android 4) working fine.
Unfortunatly, I haven't such devices, so I can't test it after every changing in source code. Maybe someone already had this problem?
Also, I tried to test this app using Samsung Remote Test Lab, but, unfortunatly, Galaxy S2 with android 2.3 was placed on the table and his camera takes blackness (splash do nothing). In this case there are no any white strips.
onCreate():
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceHolder.addCallback(this);
...
mSurfaceView.setLayoutParams(lp);
onResume():
mCamera = Camera.open();
if (mCamera == null) {
Toast.makeText(this, R.string.t_no_camera, Toast.LENGTH_SHORT).show();
finish();
} else {
mCamera.setDisplayOrientation(90);
Parameters p = mCamera.getParameters();
p.setFocusMode(Parameters.FOCUS_MODE_AUTO);
prepareFlash();
if (prepareResolution()) {
Camera.Size size = getBestPreviewSize(mDisplay.getWidth(), mDisplay.getHeight(), p);
if (size != null) {
p.setPreviewSize(size.width, size.height);
}
}
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
surfaceCreated(SurfaceHolder holder):
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
Any idea? What could it be? I will be grateful for any hint.
You start camera preview in Portrait mode; bugs with such preview occur on different devices. Sometimes, these bugs get fixed with software upgrade from the manufacturer, sometimes, they are not. Sometimes, crazy workarounds can resolve the problem.
To be on the safe side, stick with Landscape mode. Maybe your app can keep a black list of devices/build versions where the preview activity will not allow vertical (portrait) orientation.
Note that you can usually simulate portrait orientation by using rotated icons, bitmaps, and even text labels.

Trouble Accessing Front Camera on Android Galaxy S3

I have a small app I have been working on that uses the front camera. The way I have been obtaining use of the front camera seems to work on most phones, but users have been reporting trouble on the S3 and various other new devices. The way I have been accessing the front camera is like so:
// Find the ID of the front camera
CameraInfo cameraInfo = new CameraInfo();
for(int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if(cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
defaultCameraId = i;
mCameraFound = true;
}
}
if(!mCameraFound)
displayDialog(8);
From some of the error reporting I've added into the app, I've noticed the S3 actually finds the front camera, but users report it only shows a blank screen? I have only been able to test on the devices I have (GNex and N7). I was hoping someone here may have some experience with this or may be able to help me solve this issue. If you want to try the app out on your S3, check the link below. Thanks in advance.
https://play.google.com/store/apps/details?id=com.wckd_dev.mirror
EDIT: I created a MirrorView object which contains a TextureView used for the preview. The MirrorView object implements a SurfaceTextureListener. Within the onSurfaceTextureAvailable() method is where the preview is started. I also created a method for restarting the preview after the app has gone from hidden back to visible.
So this is called when the app is first started:
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.height, mPreviewSize.width);
requestLayout();
mCamera.setParameters(parameters);
mCamera.setPreviewTexture(surface);
mCamera.startPreview();
}
}
catch(RuntimeException e) {
// Log.e(TAG, "RuntimeException caused by setPreviewTexture()", exception);
}
catch (IOException e) {
// Log.e(TAG, "IOException caused by setPreviewTexture()", exception);
}
}
The restartPreview call is to an identical (but separate) method. From some of the debug data I've been collecting through users, I've noticed that the app finds two camera on the S III and selects the id matching CAMERA_FACING_FRONT. Also, this issue doesn't seem to be happening on all S III. I have users who have feedback reporting as much. The latest report from a user experiencing this issue was an AT&T S III user. Any help would be appreciated!
Got some face time with an S3 tonight that was experiencing this issue with my app. Here what was going on. The TextureView relies on 2d hardware acceleration which is supposed to on by default (from what I understood) on 4.0+ devices. It wasn't turning on (for my app at least) on his phone. The fix was as simple as adding a single line in the manifest (under application).
android:hardwareAcceleration = "true"

Setting Parameters.FLASH_MODE_TORCH doesn't work on Droid X 2.3

I am writing an app that sets the flash mode to torch. I have been testing the application on my Droid X, and the LED light does not come on. I tried it on a Droid Incredible and it worked fine. I can't figure out what the problem is. Here is part of my code for turning on torch mode.
Camera mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
I have added mCamera.startPreview(); because I read that should make a difference, but it doesn't. I also made a list of available flash modes and displayed them to the screen to make sure that my Droid X does have torch mode, and it was in the list. I even created a new application from code I found online that turns the LED flash on and off with a button. Again it worked fine on the Droid Incredible but not the Droid X. Is there something I am missing to get this to run on the Droid X, or could it be something with Gingerbread? The Droid X is running Gingerbread and the Droid Incredible is running FroYo.
There are quite a few quirks when setting FLASH_MODE_TORCH.
Often you need to start a camera preview:
Camera mCamera = Camera.open();
mCamera.startPreview();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
That may resolve it on some phones, other phones also require the preview to be drawn to a SurfaceView. This can be done by implementing SurfaceHolder.Callback interface in your activity.
See an example here.
It could be that the Droid X doesn't support Torch Mode. Try something like this:
List<String> pList = camera.getParameters().getSupportedFlashModes();
if (pList.contains(Parameters.FLASH_MODE_TORCH))
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
Refer to Issue 191453:
SurfaceTexture mDummy = new SurfaceTexture(1); // any int argument will do
camera.setPreviewTexture(mDummy);
camera.startPreview();
The only thing I found that works on the Droid X is the code presented by Siddhpura Amit part way down the page in this answer Use camera flashlight in Android. He checks the manufacturer and checks to see if it contains the string "motorola." If so, he has special code that can switch the camera Flash LED on or off. I can verify that it does work as I have a Motorola Droid X.

Categories

Resources