Com*_*are 9 android android-camera textureview android-textureview
我正在尝试(再次)创建实际上正常工作的相机预览逻辑,适用于所有场景:
android.hardware.Camera 和 android.hardware.camera2因为我minSdkVersion15岁,因为我并不特别关心表现,所以我试图用一个TextureView.并且,根据fadden在这里和这里的地方的建议,我试图setTransform()在TextureView适当的情况下使用Matrix它:
TextureView完全填充,以TextureView纵横比与预览帧宽高比不匹配的裁剪为代价在我的情况下,TextureView填充屏幕,减去状态栏和导航栏.
adjustAspectRatio()从GrafikaPlayMovieActivity.java开始,我现在有:
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);
}
Run Code Online (Sandbox Code Playgroud)
在这里,videoWidth并videoHeight有摄像头预览的大小,方法本身是对的子类实现TextureView.当我确定了相机预览尺寸以及TextureView自身调整大小后,我正在调用此方法.
这似乎很接近但不完全正确.特别是,iCanHazPhone黑客 - 翻转视频宽度和高度 - 是在黑暗中刺伤,没有这个,而SONY平板电脑Z2运行良好,Nexus 5变得可怕(拉伸预览不填满屏幕).
随着iCanHazPhone设置true,我得到在Nexus 5的好成绩:


随着iCanHazPhone集来false,我得到的东西,如:

同样,iCanHazPhone设置为false,我在SONY平板电脑Z2上获得了良好的效果:

但如果我把它翻到true,我得到:

我目前的理论是,不同的设备具有不同的默认摄像机方向,并且根据默认方向,我需要在计算中翻转预览宽度和高度.
所以,问题:
相机保证(与涉及Android硬件的任何内容一样)是否具有与默认设备方向匹配的默认方向?例如,Nexus 9可以正常iCanHazPhone设置为true,表示它不是手机与平板电脑,而是默认纵向与默认景观.
有没有更好的方法来解决这个问题?
对两个问题的回答是:使用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;
}
Run Code Online (Sandbox Code Playgroud)
其中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;
}
Run Code Online (Sandbox Code Playgroud)
传统相机的sensorOrientation:
Camera.CameraInfo.orientation
Run Code Online (Sandbox Code Playgroud)
而对于Camera2:
CameraCharacteristics#get(CameraCharacteristics.SENSOR_ORIENTATION)
Run Code Online (Sandbox Code Playgroud)
在计算相机预览方向时,您应该为compansateForMirror传递false,并在计算传统的相机JPG方向时传递true.
我已经在许多设备上测试了它 - 它似乎有效,但我不能保证这是防弹的;]
| 归档时间: |
|
| 查看次数: |
4653 次 |
| 最近记录: |