Android: resize camera preview on screen

Currently, the camera preview is stretched across the entire screen (854x480). I would like it to display (427x240).

Here is the appropriate code to implement the camera:

Camera.Parameters params = camera.getParameters(); params.setPreviewSize(427, 240); mCamera.setParameters(params); 
 public class CameraTest extends Activity { private Preview mPreview; private DrawOnTop mDrawOnTop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Hide the window title. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); // Create our Preview view and set it as the content of our activity. // Create our DrawOnTop view. mDrawOnTop = new DrawOnTop(this); mPreview = new Preview(this, mDrawOnTop); setContentView(mPreview); addContentView(mDrawOnTop, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } } 
+4
source share
2 answers

saw this on developer.android.com :

If you want to set a specific size for the camera preview, set this in the surfaceChanged () method, as noted in the comments above. when setting the preview size, you should use the values ​​from getSupportedPreviewSizes (). Do not set arbitrary values ​​in setPreviewSize ().

and wrote this to get the smallest value:

  List<Size> list = mCamera.getParameters().getSupportedPictureSizes(); Size size = list.get(0); int height = size.height; int minHeight=9999999; for(int i = 0; i<=list.size(); i++){ if(list.get(i).height<minHeight){ minHeight = list.get(i).height; } } 

hope it works!

+1
source

mCamera.setParameters (PARAMS); just set the parameters. It will be transferred to camera equipment.

To set the size of the preview window, you need to change the camera layout file.

0
source

All Articles