Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing the Camera Preview

Tags:

android

I have implemented a camera application, which will show the preview in the entire screen of the device. But my requirement is to show the Camera preview in a small screen. My device resolution is 800x480(WxH) i.e, Nexus one. I can able to show the preview in the entire screen without scale down the preview. Its coming perfectly, but when i try show the preview in small screens(part of my total mobile screen), the preview is getting stretched and not looking good.

Is there any way to show the preview correctly in a small screen. I think we need to scale down the image preview. But when i try to scale down the image preview, the android system itself doesn't allowing to set the scaled preview size.

Could any one please help me how to scale the image preview in small screen.

like image 569
uday Avatar asked Mar 31 '11 11:03

uday


People also ask

What is camera preview?

PreviewView is a subclass of FrameLayout . To display the camera feed, it uses either a SurfaceView or TextureView , provides a preview surface to the camera when it's ready, tries to keep it valid as long as the camera is using it, and when released prematurely, provides a new surface if the camera is still in use.

How to set camera preview orientation in Android?

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


1 Answers

After struggling with this same problem for about a week, I was FINALLY able to get the camera preview to resize itself programmatically! :-D

preview is the name of my SurfaceView and in this code, I have it set up so that it resizes itself to be half of the width of the RelativeLayout that it's in.

ViewGroup.LayoutParams params = preview.getLayoutParams();
RelativeLayout myRelLayout = (RelativeLayout) findViewById(R.id.myRelLayout);
params.width = (int) (myRelLayout.getWidth()/2);
preview.setLayoutParams(params);

Now for your use, you will need to look at the resolution of the preview and then scale down the height or width accordingly based on that resolution.

like image 62
Jared Avatar answered Oct 13 '22 10:10

Jared