我正在开发一个使用android原生摄像头的应用程序。我可以成功拍摄照片并在ImageView上存储和显示。我正在测试HTC desire Z、Nexus S和摩托罗拉Zoom。它适用于所有三种设备,除了Nexus S上的一个问题。当我使用Nexus S拍照时,预览图像已旋转90度。
你能告诉我如何克服这个问题吗?
我代码:public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_preview);
imgTakenPhoto = (ImageView) findViewById(R.id.imageView);
getPhotoClick();
}
private File getTempFile()
{
return new File(Environment.getExternalStorageDirectory(), "image.tmp");
}
private void getPhotoClick()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile()));
startActivityForResult(intent, REQUEST_FROM_CAMERA);
}
InputStream is=null;
@Override
protected void onActivityResult(int requestcode, int resultCode, Intent data){
if (requestcode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) {
File file=getTempFile();
try {
is=new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(is==null){
try {
Uri u = data.getData();
is=getContentResolver().openInputStream(u);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Bitmap thumbnail = BitmapFactory.decodeStream(is);
imgTakenPhoto.setImageBitmap(thumbnail);
}
}
我已经从保存照片到文件的问题中获得了帮助
多谢
任何人吗?
这个预览问题在一些手机上出现。我所知道的唯一简单的解决方法是在onCreate():
中设置横向模式setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
另一种(可能是过度)的可能性是在预览表面上覆盖另一个表面,创建自定义PreviewCallback并旋转并手动绘制每个帧到该表面,但存在性能和屏幕尺寸的问题。
以下函数将检测图像的方向:
public static int getExifOrientation(String filepath) {
int degree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
} catch (IOException ex) {
Log.e(TAG, "cannot read exif", ex);
}
if (exif != null) {
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1) {
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
}
}
在你的代码中,使用这个函数来旋转位图图像。Util类可以在https://android.googlesource.com/platform/packages/apps/Camera.git
中找到if(degree != 0){
bmp = Util.rotate(bmp, degree);
}
记住这只会纠正图像预览的方向。我希望这对你有帮助。