Camera拍照:
做照相机程序,结果写好了发现出问题了,预览的图像差90度。相关源代码如下:
于是在网上搜索,发现确实存在这样一个问题,网上的解决方法也各不一致。最后用setDisplayOrientation()方法解决了,修改后的代码如下:
首先添加了代码3,让Camera旋转90度。测试时发现虽然图像转回正常,但是上下却拉长了。进过研究修改代码1,把长宽颠倒。再测试预览正常了,但是拍照的时候获取的图片却依然是拉长的,于是修改代码2。这回拍照正常,但是仍然存在一个问题,保存的图片是横着的。
看网上很多人都遇到了这个问题,好像与系统有关,既然没法在Camera这里搞定,那么就将获取的Bitmap进行旋转。程序里通过重写PictureCallback的onPictureTaken来获取图片,代码是
在这里可以对bm进行旋转,代码如下:
这样保存的图片就正常了!
MediaRecorder录像:
同样的预览问题在录像中也出现了,但是由于是用MediaRecorder进行录像,没有Camera对象,一时比较无措。后来经过网友的提醒,想到了MediaRecorder有setCamera()的方法,于是添加如下代码:
这样预览的问题就解决了!至于网上很多人说添加mRecorder.setOrientationHint(90); 这个方法并不能改变预览的角度,这个方法会改变保存后的视频文件播放时是否是横向。根据用户的习惯,最后也添加这个代码保证在播放视频是是横向的。
细节性修改,做下记录(平板修改)
1)现象:预览发生异常,和屏幕方向不对应
解决方法:在系统中,在PhotoUI类修改成m.preRotate(180);,可以定死他的角度
2)现象:拍照后的照片与实际偏差90°
解决办法:之后在PhotoModule中修改JpegPictureCallback的角度orientation,我知道没讲明白,可以忽略
mActivity.getMediaSaveService().addImage(
jpegData, title, date, mLocation, width, height,
orientation-90, exif, mOnMediaSavedListener, mContentResolver);
3)现象:视频文件播放时是否是横向
解决办法:上面已经涉及,在VideoModule类中,修改mMediaRecorder.setOrientationHint(180);
4)现象:点击模式出现的选项方向相差90°
解决办法:布局如下,然后 在ModuleSwitchView 中修改方向(根据KEY_CAMERA_MODULE ,他这是由CameraSettings获得 ,以及 setOrientation 综合查找)
<IconListPreference
camera:key="pref_camera_module_key"
camera:defaultValue="@string/pref_camera_module_default"
camera:title="@string/pref_camera_module_title"
camera:icons="@array/pref_camera_module_icons"
camera:largeIcons="@array/pref_camera_module_icons"
camera:labelList="@array/pref_camera_module_entry_descriptions"
camera:entries="@array/pref_camera_module_entries"
camera:entryValues="@array/pref_camera_module_entryvalues" />
防止模式修改无效
boolean showCheZai = getResources().getBoolean(R.bool.config_show_chezai);
if (showCheZai) {
}else if (!showCheZai&&mOrientation == orientation){
return;
}
if (mRotateGridModule != null) {
LayoutParams lp = (FrameLayout.LayoutParams) mGridViewModule.getLayoutParams();
if (orientation % 180 == 0) {
lp.setMargins(0, CameraUtil.dpToPixel(60), 0, CameraUtil.dpToPixel(60));
} else {
lp.setMargins(CameraUtil.dpToPixel(60), 0, CameraUtil.dpToPixel(60), 0);
}
mGridViewModule.setLayoutParams(lp);
mGridViewModule.setOrientation(orientation+90, false);
mRotateGridModule.setOrientation(orientation+90, false);
}
if (mGalleryModule != null) {
mGalleryModule.setOrientation(orientation+90, false);
}
5)现象:有动态感应,根据方向不断转动,不算错误
解决办法:在CameraActivity方法修改方向,为270
@Override
public void onOrientationChanged(int orientation) {
// We keep the last known orientation. So if the user first orient
// the camera then point the camera to floor or sky, we still have
// the correct orientation.
if (orientation == ORIENTATION_UNKNOWN) {
return;
}
if (mOrientationCorrectedValue != -1)
orientation = mOrientationCorrectedValue + orientation;
mLastRawOrientation = orientation;
mCurrentModule.onOrientationChanged(270);
OnScreenHint.globalOrientaion = orientation;
if (mStorageHint != null)
{
mStorageHint.onOrientationChanged();
}
}
6 前置后置区分:
if(cameraId ==1){
mMediaRecorder.setOrientationHint(180);
}else if(cameraId ==0){
mMediaRecorder.setOrientationHint(0);
}
1为前置,2为后置
7.直接预览就出错,修改PhotoModule中的 mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation-90);
private void setDisplayOrientation() {
mDisplayRotation = CameraUtil.getDisplayRotation(mActivity);
mDisplayOrientation = CameraUtil.getDisplayOrientation(mDisplayRotation, mCameraId);
mCameraDisplayOrientation = mDisplayOrientation;
mUI.setDisplayOrientation(mDisplayOrientation);
if (mFocusManager != null) {
mFocusManager.setDisplayOrientation(mDisplayOrientation);
}
// Change the camera display orientation
if (mCameraDevice != null) {
mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation-90);
}
}