import android.hardware.Camera; import android.view.SurfaceHolder; import android.view.SurfaceView; public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder holder; private Camera camera; public CameraPreview(Context context, Camera camera) { super(context); this.camera = camera; holder = getHolder(); holder.addCallback(this); } @Override public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (holder.getSurface() == null) { return; } try { camera.stopPreview(); } catch (Exception e) { e.printStackTrace(); } try { camera.setPreviewDisplay(holder); camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); } }
public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } Camera.Parameters params = camera.getParameters(); params.setPreviewSize(640, 480); camera.setParameters(params); camera.startPreview(); }This example sets the camera preview size to 640x480 using the setPreviewSize method of the Camera.Parameters class. The setPreviewDisplay method is called before setting the preview size. Package library: android.hardware.Camera