Android中的相机API存在很大问题。我想在SurfaceView中开始相机预览。这样可行。但是预览图像是distordet。为什么会发生这种解决方案?
我在App Store中有一款像Retro Camera这样的应用。所以我想在矩形或正方形中预览相机。但是预览没有正确的解决方案。
我的代码:
public class CameraActivity extends Activity implements SurfaceHolder.Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView buttonStartCameraPreview = (ImageView)findViewById(R.id.scanning);
ImageView buttonStopCameraPreview = (ImageView)findViewById(R.id.buttonBack);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
buttonStartCameraPreview.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
if(!previewing) {
camera = Camera.open();
if(camera != null) {
try {
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
);
buttonStopCameraPreview.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
if(camera != null && previewing) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
}
);
}
@Override
public void surfaceChanged(
SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
结果:
我有一个正方形,但预览是一个矩形。