カスタムカメラクラスを作成するときにAndroidでカメラの表示方向を維持する方法. Androidデバイス以上の完璧なソリューションではない場合、このシナリオで何をするか、一部のデバイスがカメラの横向きで行う場合、または一部のデバイスがカメラの縦向きで行う場合。
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();
}