Najib Puthawala Najib Puthawala - 19 days ago 5
Android Question

Custom Camera display orientation in android

How to maintain camera display orientation in android when i create custom camera class.
when i not perfect solution for more then android device then what i do for this scenarios and when some device do on camera landscape or some device do on camera portrait.

public CameraSurface(Context context) {
super(context);

try {
holder = this.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

} catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
// Open the Camera in preview mode
this.camera = Camera.open();
this.camera.setPreviewDisplay(holder);
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when replaced with a new screen
// Always make sure to release the Camera instance
camera.stopPreview();
camera.release();
camera = null;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
// Camera.Parameters parameters = camera.getParameters();
// System.out.println("height...."+width+" height....."+height);
// parameters.setPreviewSize(width, height);
// camera.setParameters(parameters);
// camera.startPreview();
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

// You need to choose the most appropriate previewSize for your app
Camera.Size previewSize;
if(previewSizes.size() > 1){
previewSize =previewSizes.get(1); // .... select one of previewSizes here
}else{
previewSize =previewSizes.get(0);
}
parameters.setPreviewSize(previewSize.width, previewSize.height);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
camera.startPreview();
}

Answer

Following two links will help you:

Using Camera API.
Android Camera Preview is sideways.

Comments