Android: Taking and saving picture, portrait mode/landscape mode - android

When I am in portait mode with my android camera, the image seen is in landscape
So I wrote :
Method rotateMethod;
rotateMethod = android.hardware.Camera.class.getMethod("setDisplayOrientation", int.class);
rotateMethod.invoke(camera, 90);
It works. Yet, when I save the image, the image his in landscape (width is bigger than height)
What could I do to collect the image (bytes[]) of what my camera saw ?

Use this code:
Parameters p=camera.getParameters();
p.set("orientation", "portrait");
camera.setParameters(p);
and start camera preview

Related

Crop Image on the fly

I am working on a camera app.The camera preview show is with 4:3 ratio,When i take a photo i get the complete image or full screen image.
Since i am showing the user a Camera Preview with 4:3 ratio, i want the user to also get the image with 4:3 ratio only.
I have seen many libraries which gives the user an option to crop, but here as i give path of the image and then it should crop the image and display it to user.
How can i achieve this ?
I don't think android has an out of the box cropper - but you can crop your own with Bitmap.createBitmap(originalBitmap, x1, y1, x2, y2)
Each Camera on each Android device has List of Preview Sizes and Picture Sizes.
Camera.Parameters parameters = camera.getParameters();
List<Size> preview_sizes = parameters.getSupportedPreviewSizes();
List<Size> pictures_sizes = parameters.getSupportedPictureSizes();
Try to loop all this Sizes and pick 4:3. So your result will be 4:3 without cropping.
Read more here.

Android Camera 2, how to crop preview size

I'm working on Camera 2 API recently and my device has a 16:9 screen ratio but my camera sensor is 4:3. So all the preview size I got is 4:3. I' wondering is there any way I get a crop the size and only display the 16:9 part? I tried a long time and didn't find any help for camera 2.
My current camera code is similar to the camera 2 basic sample.
So how should I crop the preview and only display the 16:9 part on the texture view?
Thanks!!
You could create a SurfaceTexture with your ratio 4:3 instead of 16:9.
You could use the custom view from that sample project or create your own with the new ConstraintLayout and setting the ratio of height from 16:9 of your width (or inverse).
Then when you set the size of your texture:
texture.setDefaultBufferSize(width, height)
You will get not problems streching, because your texture is the same ratio as your camera output.
Hope this helps
I answered a similar question after having this same problem, and not being able to change the aspect ratio of the actual view to match the camera output (as Sulfkain's answer suggests).
Tl;dr: the answer lies in the configureTransform in Camera2BasicFragment. Although their function is mostly implemented for orientation, the scaling matrix can resolve scaling/aspect ratio issues with views that are the wrong shape for the camera output.
You have to do some debug on
setUpCameraOutputs(int width, int height)
and check the part that you are selecting the size of the output
// Danger, W.R.! Attempting to use too large a preview size could exceed the camera
// bus' bandwidth limitation, resulting in gorgeous previews but the storage of
// garbage capture data.
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedPreviewWidth,
rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest);
maybe you have to hard select something there.

How can I set the width and height of my custom camera?

I ve created my custom camera on my app.When I press capture button it saves the taken photo where I set it before.
Everything is fine for now. But the problem is the phone saves the photo as phones pixels I mean I want to 600x600 pixel but everyphone saves the picture as its camera defaults.
How can I solve this?
I use this example for my custom camera : link
You must have to re size the bitmap of of your taken picture Like...
Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
then after delete the previous image which one was taken by camera
Whats happen in your case:
1.) always custom camera returns it default preview size you can get your preview and picture size using the code and chooses as depending on your requirement.
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
List<Camera.Size> sizes = params.getSupportedPictureSizes()

Why is the default android camera preview smoother than my own camera preview?

I just setup a very basic camera preview that displays the camera full screen. I compared the smoothness of both my app and the android camera and figured that the android camera seems a lot smoother.
Why is that the case? Are there any special tricks to improve the camera preview performance?
I faced the same issue a time ago, and it turned out to be related with the camera resolution. Chances are that your Camera is initializing with the maximum available resolution, which may slow down performance during preview. Try and set a lower picture size with something like this.-
Camera.Parameters params = camera.getParameters();
params.setPictureSize(1280, 960);
camera.setParameters(params);
Notice that you'll need to set an available picture size. You can check available sizes with
camera.getParameters().getSupportedPictureSizes();
Hope it helps.
EDIT
It seems using a picture size with different aspect ratio than the default one, slows down performance as well. This is how I choose my pictureSize.-
First off, I get the aspect ratio of the camera default pictureSize
Camera.Parameters params = camera.getParameters();
defaultCameraRatio = (float) params.getPictureSize().width / (float) params.getPictureSize().height;
And then I get a lower pictureSize that fits the same ratio.-
private Size getPreferredPictureSize() {
Size res = null;
List<Size> sizes = camera.getParameters().getSupportedPictureSizes();
for (Size s : sizes) {
float ratio = (float) s.width / (float) s.height;
if (ratio == defaultCameraRatio && s.height <= PHOTO_HEIGHT_THRESHOLD) {
res = s;
break;
}
}
return res;
}
Where PHOTO_HEIGHT_THRESHOLD is the max height you want to allow.
This answer is a bit late, but after struggling with the same problem, I figured I'd share what I found.
Not only was I unable to match the stock camera's "smoothness", but I was also unable to match the metering of the stock camera. My live preview was much darker compared to the stock camera, especially in low light situations.
My solution was ultimately an FPS issue, as originally proposed by Robin in an early comment.
The default preview FPS on the Nexus 5 is a static 15 FPS. The stock android app detects and sets a preview FPS that uses a dynamic range (as long as that range includes 30fps). On a nexus 5, that range is 7fps -> 30fps. In low light the camera drops to a lower FPS to keep the preview bright, while in bright conditions it jumps to a smooth 30 fps.
Relevant code from the stock android photo app:
The call to set the FPS starts on line 1560 of com.android.camera.PhotoModule (https://android.googlesource.com/platform/packages/apps/Camera2/+/android-4.4.4_r2.0.1/src/com/android/camera/PhotoModule.java).
The utility it uses to identify the ideal FPS starts on line 833 of com.android.camera.util.CameraUtil (https://android.googlesource.com/platform/packages/apps/Camera2/+/android-4.4.4_r2.0.1/src/com/android/camera/util/CameraUtil.java)
Here's are the camera api calls to set the FPS using hard coded values (for the sake of simplicity) specific to a Nexus 5.
public void setFps(Camera camera) {
Camera.Parameters params = camera.getParameters();
params.setPreviewFpsRange(7000, 30000);
camera.setParameters(params);
}

Change orientation of camera preview in android depending on the orentation of the layout

I display the Camera preview in surfaceView. In the surface callback I have this code in the surfaceChanged function:
if(getResources().getConfiguration().orientation== getResources().getConfiguration().ORIENTATION_LANDSCAPE)
camera.setDisplayOrientation(0);
else
camera.setDisplayOrientation(90);
This works ok it rotates the camera in portrait orientation. But the preview is wrongly orientated in when I change between I turn the tablet for 180 deegres. Is there a property which whether tablet is turn up or down, so that I can change the orientation recordingly.
You can use getRotation()
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getRotation();

Categories

Resources