Android9上的修改,需求是把竖屏转横屏,系统方向和摄像头方向都转了90度之后,骁龙相机的方向还是竖屏的,陆陆续续改了三次,终于差不多了。(希望是可以了)
梳理一下修改历程。
首先,让骁龙相机整体横屏,需要修改xml
--- a/packages/apps/SnapdragonCamera/AndroidManifest.xml
+++ b/packages/apps/SnapdragonCamera/AndroidManifest.xml
@@ -50,7 +50,7 @@
android:label="@string/snapcam_app_name"
android:launchMode="singleTask"
android:logo="@mipmap/ic_launcher_gallery"
- android:screenOrientation="portrait"
+ android:screenOrientation="landscape"
android:taskAffinity="com.android.camera.CameraActivity"
android:theme="@style/Theme.Camera"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
@@ -150,7 +150,7 @@
android:label="@string/snapcam_app_name"
android:launchMode="singleInstance"
android:logo="@mipmap/ic_launcher_gallery"
- android:screenOrientation="portrait"
+ android:screenOrientation="landscape"
android:taskAffinity="com.android.camera.SecureCameraActivity"
android:theme="@style/Theme.Camera"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan" >
但是仅仅这样修改,出现了预览画面大小的问题。大概是这样的:
后面又出现了出现黑边,显示画面不全等问题,通过增加log一通操作,最终修改如下:
--- a/packages/apps/SnapdragonCamera/src/com/android/camera/PhotoUI.java
+++ b/packages/apps/SnapdragonCamera/src/com/android/camera/PhotoUI.java
@@ -408,15 +408,22 @@ public class PhotoUI implements PieListener,
} else {
float width = mMaxPreviewWidth, height = mMaxPreviewHeight;
if (width == 0 || height == 0) return;
- if(mScreenRatio == CameraUtil.RATIO_4_3)
- height -= (mTopMargin + mBottomMargin);
+ //if(mScreenRatio == CameraUtil.RATIO_4_3)
+ //height -= (mTopMargin + mBottomMargin-20);
if (mOrientationResize) {
- scaledTextureWidth = height * mAspectRatio;
+ /*scaledTextureWidth = height * mAspectRatio;
if (scaledTextureWidth > width) {
scaledTextureWidth = width;
scaledTextureHeight = scaledTextureWidth / mAspectRatio;
} else {
+ scaledTextureHeight = height;
+ }*/
+ scaledTextureHeight = width * mAspectRatio;
+ if (scaledTextureHeight > height) {
scaledTextureHeight = height;
+ scaledTextureWidth = scaledTextureHeight / mAspectRatio;
+ } else {
+ scaledTextureWidth = width;
}
} else {
if (width > height) {
@@ -443,15 +450,16 @@ public class PhotoUI implements PieListener,
if (((rotation == 0 || rotation == 180) && scaledTextureWidth > scaledTextureHeight)
|| ((rotation == 90 || rotation == 270)
&& scaledTextureWidth < scaledTextureHeight)) {
- lp = new FrameLayout.LayoutParams((int) scaledTextureHeight,
- (int) scaledTextureWidth, Gravity.CENTER);
+ lp = new FrameLayout.LayoutParams((int) scaledTextureWidth,
+ (int) scaledTextureHeight, Gravity.LEFT);
} else {
lp = new FrameLayout.LayoutParams((int) scaledTextureWidth,
- (int) scaledTextureHeight, Gravity.CENTER);
+ (int) scaledTextureHeight, Gravity.LEFT);
}
if(mScreenRatio == CameraUtil.RATIO_4_3) {
- lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
- lp.setMargins(0, mTopMargin, 0, mBottomMargin);
+ lp.gravity = Gravity.LEFT | Gravity.TOP;
+ lp.setMargins(mTopMargin, 0,mBottomMargin, 0);
+ //lp.setMargins(0, mTopMargin, 0, mBottomMargin);
}
}
参照拍照预览,修改了录像预览
--- a/packages/apps/SnapdragonCamera/src/com/android/camera/VideoUI.java
+++ b/packages/apps/SnapdragonCamera/src/com/android/camera/VideoUI.java
@@ -382,6 +382,7 @@ public class VideoUI implements PieRenderer.PieListener,
} else {
ratio = (float) height / width;
}
if (mOrientationResize &&
mActivity.getResources().getConfiguration().orientation
!= Configuration.ORIENTATION_PORTRAIT) {
@@ -436,13 +437,13 @@ public class VideoUI implements PieRenderer.PieListener,
} else {
float width = mMaxPreviewWidth, height = mMaxPreviewHeight;
if (width == 0 || height == 0) return;
- if(mScreenRatio == CameraUtil.RATIO_4_3)
- height -= (mTopMargin + mBottomMargin);
+ //if(mScreenRatio == CameraUtil.RATIO_4_3)
+ //height -= (mTopMargin + mBottomMargin);
if (mOrientationResize) {
- scaledTextureWidth = height * mAspectRatio;
+ scaledTextureWidth = height / mAspectRatio;
if (scaledTextureWidth > width) {
scaledTextureWidth = width;
- scaledTextureHeight = scaledTextureWidth / mAspectRatio;
+ scaledTextureHeight = scaledTextureWidth * mAspectRatio;
} else {
scaledTextureHeight = height;
}
@@ -468,15 +469,14 @@ public class VideoUI implements PieRenderer.PieListener,
Log.v(TAG, "setTransformMatrix: scaledTextureWidth = " + scaledTextureWidth
+ ", scaledTextureHeight = " + scaledTextureHeight);
if (((rotation == 0 || rotation == 180) && scaledTextureWidth > scaledTextureHeight)
|| ((rotation == 90 || rotation == 270)
&& scaledTextureWidth < scaledTextureHeight)) {
- lp = new FrameLayout.LayoutParams((int) scaledTextureHeight,
- (int) scaledTextureWidth, Gravity.CENTER);
+ lp = new FrameLayout.LayoutParams((int) scaledTextureWidth,
+ (int) scaledTextureHeight, Gravity.LEFT);
} else {
lp = new FrameLayout.LayoutParams((int) scaledTextureWidth,
- (int) scaledTextureHeight, Gravity.CENTER);
+ (int) scaledTextureHeight, Gravity.LEFT);
}
}
接着发现全景也有问题,全景拍摄的结果是正常的,但是预览旋转了90度。
修改如下:
--- a/packages/apps/SnapdragonCamera/src/com/android/camera/WideAnglePanoramaUI.java
+++ b/packages/apps/SnapdragonCamera/src/com/android/camera/WideAnglePanoramaUI.java
@@ -377,16 +377,15 @@ public class WideAnglePanoramaUI implements
Point size = new Point();
display.getSize(size);
- int width = size.x;
- int height = size.y;
+ int width = size.y;
+ int height = size.x;
int xOffset = 0;
int yOffset = 0;
int w = width;
int h = height;
-
- h = w * 4 / 3;
- yOffset = (height - h) / 2;
-
+ //h = w * 4 / 3;
+ w = h * 3 / 4;
+ yOffset = (width - w) / 2;
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(w, h);
mTextureView.setLayoutParams(param);
mTextureView.setX(xOffset);
@@ -546,9 +545,9 @@ public class WideAnglePanoramaUI implements
// Rotation needed to display image correctly on current display
int rotation = (cameraOrientation - displayRotation + 360) % 360;
if (rotation >= 180) {
- mTextureView.setRotation(180);
+ mTextureView.setRotation(90);
} else {
- mTextureView.setRotation(0);
+ mTextureView.setRotation(270);
}
}
最后,意外地发现,前摄的镜像也失效了。迅速锁定了函数flipJpeg,通过log发现orientation的值导致没有镜像,改。。。
--- a/packages/apps/SnapdragonCamera/src/com/android/camera/PhotoModule.java
+++ b/packages/apps/SnapdragonCamera/src/com/android/camera/PhotoModule.java
@@ -1356,7 +1356,7 @@ public class PhotoModule
private byte[] flipJpeg(byte[] jpegData, int orientation, int jpegOrientation) {
Bitmap srcBitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
Matrix m = new Matrix();
- if(orientation == 270 || orientation == 90) {
+ if(orientation == 0 || orientation == 180) {
// Judge whether the picture or phone is horizontal screen
if (jpegOrientation == 0 || jpegOrientation == 180) {
m.preScale(-1, 1);
横屏改得跌宕起伏,遭遇了很多奇奇怪怪的现象,最后勉强改到了正常的效果。不过总觉得这种修改不至于修改这么大面积,但愿早日搞清骁龙相机!