Java 根据textureview的大小自动调整camera2预览的大小
我正在为我的项目使用Camera2API。预览设置在谷歌推荐的AutofitTextureView上Java 根据textureview的大小自动调整camera2预览的大小,java,android,android-camera,android-camera2,textureview,Java,Android,Android Camera,Android Camera2,Textureview,我正在为我的项目使用Camera2API。预览设置在谷歌推荐的AutofitTextureView上 但是使用他们的代码,当textureview填满整个屏幕时,预览被拉伸了。因此我找到了stackoverflow答案,并将代码编辑为: AutoFitTextureView.java: package com.example.android.camera2basic; import android.content.Context; import android.util
但是使用他们的代码,当textureview填满整个屏幕时,预览被拉伸了。
因此我找到了stackoverflow答案,并将代码编辑为:
AutoFitTextureView.java:
package com.example.android.camera2basic;
import android.content.Context;
import android.util.AttributeSet;
import android.view.TextureView;
public class AutoFitTextureView extends TextureView {
private int mRatioWidth = 0;
private int mRatioHeight = 0;
public AutoFitTextureView(Context context) {
this(context, null);
}
public AutoFitTextureView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setAspectRatio(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;
mRatioHeight = height;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
#This is the line that I have changed:
if (width > height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
}
package com.example.android.camera2base;
导入android.content.Context;
导入android.util.AttributeSet;
导入android.view.TextureView;
公共类AutoFitTextureView扩展了TextureView{
私有宽度=0;
私有内部高度=0;
公共AutoFitTextureView(上下文){
这个(上下文,空);
}
公共AutoFitTextureView(上下文、属性集属性){
这(上下文,属性,0);
}
公共AutoFitTextureView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
公共无效设置AspectRatio(内部宽度、内部高度){
如果(宽度<0 | |高度<0){
抛出新的IllegalArgumentException(“大小不能为负”);
}
宽度=宽度;
高度=高度;
requestLayout();
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
int width=MeasureSpec.getSize(widthmasurespec);
int height=MeasureSpec.getSize(heightMeasureSpec);
如果(0==mRatioWidth | | 0==mRatioHeight){
设置测量尺寸(宽度、高度);
}否则{
#这是我更改的行:
如果(宽度>高度*mRatioWidth/mRatioHeight){
设置测量尺寸(宽度、宽度*mRatioHeight/mRatioWidth);
}否则{
设置测量尺寸(高度*mRatioWidth/mRatioHeight,高度);
}
}
}
}
使用上述代码和此代码:
private static final int MAX_PREVIEW_WIDTH = 1920;
private static final int MAX_PREVIEW_HEIGHT = 1080;
private Size previewSize;
int displayRotation = getWindowManager().getDefaultDisplay().getRotation();
int mSensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
boolean swappedDimensions = false;
switch (displayRotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_180:
if (mSensorOrientation == 90 || mSensorOrientation == 270) {
swappedDimensions = true;
}
break;
case Surface.ROTATION_90:
case Surface.ROTATION_270:
if (mSensorOrientation == 0 || mSensorOrientation == 180) {
swappedDimensions = true;
}
break;
default:
//Log.e(TAG, "Display rotation is invalid: " + displayRotation);
}
Point displaySize = new Point();
getWindowManager().getDefaultDisplay().getSize(displaySize);
int rotatedPreviewWidth = width;
int rotatedPreviewHeight = height;
int maxPreviewWidth = displaySize.x;
int maxPreviewHeight = displaySize.y;
if (swappedDimensions) {
rotatedPreviewWidth = height;
rotatedPreviewHeight = width;
maxPreviewWidth = displaySize.y;
maxPreviewHeight = displaySize.x;
}
if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
maxPreviewWidth = MAX_PREVIEW_WIDTH;
}
if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
maxPreviewHeight = MAX_PREVIEW_HEIGHT;
}
Size largest = Collections.max(Arrays.asList(map.getOutputSizes(SurfaceTexture.class)), new PrismaCamera.CompareSizesByArea());
previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest);
public static Size chooseOptimalSize(Size[] choices, int textureViewWidth, int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio) {
// Collect the supported resolutions that are at least as big as the preview Surface
List<Size> bigEnough = new ArrayList<>();
// Collect the supported resolutions that are smaller than the preview Surface
List<Size> notBigEnough = new ArrayList<>();
int w = aspectRatio.getWidth();
int h = aspectRatio.getHeight();
for (Size option : choices) {
if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight &&
option.getHeight() == option.getWidth() * h / w) {
if (option.getWidth() >= textureViewWidth &&
option.getHeight() >= textureViewHeight) {
bigEnough.add(option);
} else {
notBigEnough.add(option);
}
}
}
// Pick the smallest of those big enough. If there is no one big enough, pick the
// largest of those not big enough.
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizesByArea());
} else if (notBigEnough.size() > 0) {
return Collections.max(notBigEnough, new CompareSizesByArea());
} else {
Log.e(TAG, "Couldn't find any suitable preview size");
return choices[0];
}
}
private static final int MAX_PREVIEW_WIDTH=1920;
专用静态最终整数最大预览高度=1080;
私有大小预览大小;
int displayRotation=getWindowManager().getDefaultDisplay().getRotation();
int mSensorOrientation=cameraCharacteristics.get(cameraCharacteristics.SENSOR\u方向);
布尔交换维度=false;
开关(显示旋转){
外壳表面旋转0:
外壳表面旋转180度:
如果(mSensorOrientation==90 | | mSensorOrientation==270){
交换维度=真;
}
打破
外壳表面旋转90度:
外壳表面。旋转_270:
如果(mSensorOrientation==0 | | mSensorOrientation==180){
交换维度=真;
}
打破
违约:
//Log.e(标记“显示旋转无效:”+displayRotation);
}
点显示大小=新点();
getWindowManager().getDefaultDisplay().getSize(displaySize);
int rotatedPreviewWidth=宽度;
int rotatedPreviewHeight=高度;
int maxPreviewWidth=displaySize.x;
int maxPreviewHeight=displaySize.y;
如果(交换的维度){
旋转预览宽度=高度;
旋转预览时的光=宽度;
maxPreviewWidth=displaySize.y;
MaxPreviewWight=displaySize.x;
}
如果(最大预览宽度>最大预览宽度){
maxPreviewWidth=最大预览宽度;
}
如果(最大预览高度>最大预览高度){
最大预览高度=最大预览高度;
}
Size max=Collections.max(Arrays.asList(map.getOutputSizes(SurfaceTexture.class)),新的PrismaCamera.CompareSizesByArea();
previewSize=chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class)),
旋转预览宽度,旋转预览宽度,最大预览宽度,最大预览宽度,最大);
公共静态大小选择最佳大小(大小[]选项,int-TextureView宽度,int-TextureView宽度,int-maxWidth,int-maxHeight,大小aspectRatio){
//收集至少与预览曲面一样大的支持分辨率
List bigtough=new ArrayList();
//收集小于预览曲面的支持分辨率
List notbigtough=new ArrayList();
int w=aspectRatio.getWidth();
int h=aspectRatio.getHeight();
用于(大小选项:选项){
if(option.getWidth()=TextureView){
足够大。添加(选项);
}否则{
不足够大。添加(选项);
}
}
}
//选择那些足够大的最小的。如果没有足够大的人,选择
//最大的那些不够大。
如果(足够大。大小()>0){
return Collections.min(足够大,新的CompareSizesByArea());
}else if(notbigtough.size()>0){
返回Collections.max(不够大,新的CompareSizesByArea());
}否则{
Log.e(标记“找不到任何合适的预览大小”);
返回选项[0];
}
}
我能够让相机预览到全屏,而不需要拉伸预览。但问题是当TextureView的宽度和高度设置为match_p之外的值时
<com.camera.AutoFitTextureView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textureView"
/>
<com.camera.AutoFitTextureView
android:layout_width="500dp"
android:layout_height="500dp"
android:id="@+id/textureView"
/>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@android:color/black">
<TextureView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/textureView"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>