============问题描述============
我使用的是竖屏预览摄像头,但是预览的图像有90度反转,但是拍摄的图片正常也是90度反转。
如果使用横屏预览,图像预览正常,但是拍出的图片跟预览是90度反转的。
现在我想在竖屏的情况下能够预览和拍摄都正常,如何去做?
下面是代码:
Button takebtn;
Button savebtn;
Button cancelbtn;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Camera camera;
File picture;
String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera);
initWigdt();
}
protected void initWigdt() {
takebtn = (Button) findViewById(R.id.takebtn);
savebtn = (Button) findViewById(R.id.savebtn);
cancelbtn = (Button) findViewById(R.id.cancelbtn);
surfaceView = (SurfaceView) findViewById(R.id.surview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(surfaceCallback);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
takebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
takePic();
}
});
}
// 保存图片
private void takePic() {
camera.takePicture(null, null, pictureCallback);
}
// 显示图片
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SavePictureTask().execute(data);
camera.startPreview();
}
};
// 保存至手机卡 中
class SavePictureTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... params) {
String name = String.valueOf(new Date().getTime());
url = "/sdcard/" + name + ".jpg";
File picture = new File(url);
try {
FileOutputStream fos = new FileOutputStream(picture.getPath());
fos.write(params[0]);
fos.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("save", e.getMessage());
}
System.out.println("照片保存完成");
Intent aintent = new Intent(CameraActivity.this, MainActivity.class);
aintent.putExtra("url", url);
setResult(1, aintent);
CameraActivity.this.finish();
return null;
}
}
protected void setDisplayOrientation(Camera camera, int angle) {
Method downPolymorphic;
try {
downPolymorphic = camera.getClass().getMethod(
"setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(camera, new Object[] { angle });
} catch (Exception e1) {
e1.printStackTrace();
}
}
SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
System.out.println("摄像头open完成");
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
camera.release();
camera = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
if (Integer.parseInt(Build.VERSION.SDK) >= 8) {// 判断系统版本是否大于等于2.2
setDisplayOrientation(camera, 90);// 旋转90,前提是当前页portrait,纵向
Log.e("a", "cc");
} else { // 系统版本在2.2以下的采用下面的方式旋转
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
parameters.set("orientation", "portrait");
parameters.set("rotation", 90);
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "landscape");
parameters.set("rotation", 90);
}
Log.e("c", "dd");
}
camera.setDisplayOrientation(0);
camera.setParameters(parameters);
camera.startPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
};
============解决方案1============
http://asysbang.com/forum.php?mod=viewthread&tid=58&extra=page%3D1