gpt4 book ai didi

android - 如何确定相机预览框的默认方向?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:35 27 4
gpt4 key购买 nike

我正在(再次)尝试为所有场景创建实际工作正常的相机预览逻辑:

  • 任何设备:手机、平板电脑、 toastr 等等
  • 任何摄像头:前置、后置、侧置、面向狗等等
  • android.hardware.Cameraandroid.hardware.camera2
  • 纵向和横向设备方向

因为我的 minSdkVersion 是 15,而且我不是特别关心性能,所以我尝试使用 TextureView。并且,按照法登在 here 等地方的建议和 here ,我正在尝试在该 TextureView 上使用 setTransform() 和适当的 Matrix :

  • 正确定位预览,考虑设备方向
  • 完全填充 TextureView,代价是在 TextureView 纵横比与预览帧纵横比不匹配的地方裁剪
  • 不拉伸(stretch)图像,因此方形项目(例如 3"方形便利贴®)的预览在预览中显示为方形

在我的例子中,TextureView 填满了屏幕,去掉了状态栏和导航栏。

Grafika's PlayMovieActivity.javaadjustAspectRatio() 开始,我现在有这个:

  private void adjustAspectRatio(int videoWidth, int videoHeight,
int rotation) {
if (iCanHazPhone) {
int temp=videoWidth;
videoWidth=videoHeight;
videoHeight=temp;
}

int viewWidth=getWidth();
int viewHeight=getHeight();
double aspectRatio=(double)videoHeight/(double)videoWidth;
int newWidth, newHeight;

if (getHeight()>(int)(viewWidth*aspectRatio)) {
newWidth=(int)(viewHeight/aspectRatio);
newHeight=viewHeight;
}
else {
newWidth=viewWidth;
newHeight=(int)(viewWidth*aspectRatio);
}

int xoff=(viewWidth-newWidth)/2;
int yoff=(viewHeight-newHeight)/2;

Matrix txform=new Matrix();

getTransform(txform);

float xscale=(float)newWidth/(float)viewWidth;
float yscale=(float)newHeight/(float)viewHeight;

txform.setScale(xscale, yscale);

switch(rotation) {
case Surface.ROTATION_90:
txform.postRotate(270, newWidth/2, newHeight/2);
break;

case Surface.ROTATION_270:
txform.postRotate(90, newWidth/2, newHeight/2);
break;
}

txform.postTranslate(xoff, yoff);

setTransform(txform);
}

这里,videoWidthvideoHeight 是相机预览的大小,方法本身是在TextureView 的子类上实现的。当我确定相机预览大小是什么以及调整 TextureView 本身的大小后,我将调用此方法。

这看起来很接近但不完全正确。特别是 iCanHazPhone 黑客攻击——翻转视频的宽度和高度——是在黑暗中进行的攻击,因为没有这个,虽然 SONY Tablet Z2 运行良好,但 Nexus 5 却很糟糕(拉伸(stretch)预览不填满屏幕)。

iCanHazPhone 设置为 true,我在 Nexus 5 上获得了不错的结果:

Nexus 5, Camera2 Preview, Landscape, Rear-Facing Camera

Nexus 5, Camera2 Preview, Portrait, Rear-Facing Camera

iCanHazPhone 设置为 false 时,我得到如下内容:

Nexus 5, Camera2 Preview, Portrait, Rear-Facing Camera, Stretched

同样,将 iCanHazPhone 设置为 false,我在 SONY Tablet Z2 上获得了不错的结果:

SONY Tablet Z2, Camera2 Preview, Landscape, Rear-Facing Camera

但是如果我将它翻转为 true,我会得到:

SONY Tablet Z2, Camera2 Preview, Landscape, Rear-Facing Camera, Stretched

我目前的理论是,不同的设备有不同的默认相机方向,根据默认方向,我需要在计算中翻转预览宽度和高度。

那么,问题:

  1. 是否保证摄像头(与涉及 Android 硬件的任何东西一样)具有与默认设备方向相匹配的默认方向?例如,Nexus 9 在 iCanHazPhone 设置为 true 时可以正常工作,这表明它不是手机与平板电脑,而是默认纵向与默认横向。

  2. 有没有更好的方法来处理这个问题?

最佳答案

这两个问题的答案是:使用 Camera/Camera2 API 提供的传感器方向来调整预览图像。

要计算相对于屏幕的相机旋转(可用于转换预览),我使用:

static int getRelativeImageOrientation(int displayRotation, int sensorOrientation,
boolean isFrontFacing, boolean compensateForMirroring) {
int result;
if (isFrontFacing) {
result = (sensorOrientation + displayRotation) % 360;
if (compensateForMirroring) {
result = (360 - result) % 360;
}
} else {
result = (sensorOrientation - displayRotation + 360) % 360;
}
return result;
}

其中 displayRotation 是当前显示旋转:

static int getDisplayRotation(Context context) {
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
int rotation = windowManager.getDefaultDisplay().getRotation();
switch (rotation) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
}
return 0;
}
旧相机的

sensorOrientation:

Camera.CameraInfo.orientation

对于 Camera2:

CameraCharacteristics#get(CameraCharacteristics.SENSOR_ORIENTATION)

在计算相机预览方向时,您应该为 compansateForMirror 传递 false,在计算旧相机 JPG 方向时传递 true

我已经在多种设备上对此进行了测试 - 它似乎有效,但我不能保证它是防弹的;]

关于android - 如何确定相机预览框的默认方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31839021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com