Java 我的相机预览被拉伸和挤压。我怎样才能解决这个问题?
(我已经阅读了类似问题的其他答案,但它们对我没有帮助)。 我是Android Camera 2 API的新手,我正在尝试创建一个自定义摄像头。 一切正常,但预览是拉伸时,手机在纵向模式,它是挤压时,手机在横向模式。 那么,我该如何解决我的问题呢 这里有可能包含错误的代码Java 我的相机预览被拉伸和挤压。我怎样才能解决这个问题?,java,android,android-camera2,Java,Android,Android Camera2,(我已经阅读了类似问题的其他答案,但它们对我没有帮助)。 我是Android Camera 2 API的新手,我正在尝试创建一个自定义摄像头。 一切正常,但预览是拉伸时,手机在纵向模式,它是挤压时,手机在横向模式。 那么,我该如何解决我的问题呢 这里有可能包含错误的代码 private void setUpCameraOutputs(int width, int height) { try { CameraCharacteristics characteristics
private void setUpCameraOutputs(int width, int height) {
try {
CameraCharacteristics characteristics = this.cameraManager.getCameraCharacteristics(this.cameraId);
StreamConfigurationMap map = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map != null) {
this.imageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 2);
this.imageReader.setOnImageAvailableListener(
this.imageReaderListener,
this.backgroundHandler);
Point displaySize = new Point();
this.getWindowManager().getDefaultDisplay().getSize(displaySize);
int maxPreviewWidth = displaySize.x;
int maxPreviewHeight = displaySize.y;
if (MAX_PREVIEW_WIDTH < maxPreviewWidth) {
maxPreviewWidth = MAX_PREVIEW_WIDTH;
}
if (MAX_PREVIEW_HEIGHT < maxPreviewHeight) {
maxPreviewHeight = MAX_PREVIEW_HEIGHT;
}
//Viene selezionata la risoluzione ideale per l'anteprima
this.previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
width, height, maxPreviewWidth, maxPreviewHeight);
}
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (NullPointerException e) {
//L'eccezione è lanciata quando le Camera2API sono usate,
//ma non sono supportate dal dispositivo
this.showToast("Camera2 API not supported on this device");
}
private void setUpCameraOutputs(整数宽度、整数高度){
试一试{
CameraCharacteristics characteristics=this.cameraManager.getCameraCharacteristics(this.cameraRAID);
StreamConfigurationMap=characteristics.get(
摄像机特性。定标器\流\配置\地图);
if(map!=null){
this.imageReader=imageReader.newInstance(宽度、高度、ImageFormat.JPEG,2);
this.imageReader.setOnImageAvailableListener(
这个.imageReaderListener,
这是《背景处理程序》);
点显示大小=新点();
这个.getWindowManager().getDefaultDisplay().getSize(displaySize);
int maxPreviewWidth=displaySize.x;
int maxPreviewHeight=displaySize.y;
如果(最大预览宽度<最大预览宽度){
maxPreviewWidth=最大预览宽度;
}
if(最大预览高度<最大预览高度){
最大预览高度=最大预览高度;
}
//我的理想是什么
this.previewSize=chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class)),
宽度、高度、最大预览宽度、最大预览宽度);
}
}捕获(CameraAccessE异常){
e、 printStackTrace();
}捕获(NullPointerException e){
//拉齐奥·兰西亚塔·卡梅拉2API sono usate酒店,
//ma非sono支持的dal处置
this.showtoos(“此设备不支持Camera2 API”);
}
}
在这个方法中,我找到了预览的最佳分辨率
private static Size chooseOptimalSize(Size[] choices, int
textureViewWidth, int textureViewHeight,
int maxWidth, int maxHeight) {
//Raccoglie tutte le risoluzioni grandi almeno quanto la superficie di anteprima
List<Size> bigEnough = new ArrayList<>();
//Raccoglie tutte le risoluzioni più piccole della superficie di anteprima
List<Size> notBigEnough = new ArrayList<>();
//int w = aspectRatio.getWidth();
int w = maxWidth;
//int h = aspectRatio.getHeight();
int h = maxHeight;
for (Size option : choices) {
if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight)
if (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 enoughPrende la risoluzione minima tra quelle grandi abbastanza.
//Se non ce ne sono di grandi abbastanza prende quella più grande tra quelle non
//abbastanza grandi
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizesByArea());
} else if (notBigEnough.size() > 0) {
return Collections.max(notBigEnough, new CompareSizesByArea());
} else {
Log.e("Camera2", "Couldn't find any suitable preview size");
return choices[0];
}
}
private static Size chooseOptimalSize(Size[]选项,int
纹理视图宽度,内部纹理视图宽度,
int maxWidth,int maxHeight){
//拉科利·图特·勒里索卢齐奥尼·阿尔梅诺·奎托·拉科利·勒里索卢齐奥尼·阿尔梅诺·奎托·拉科利·图特·勒里索卢齐奥尼·阿尔梅诺·奎托·拉科利·勒里索卢齐奥尼·阿梅
List bigtough=new ArrayList();
//拉科利·图特·里索卢齐奥尼·皮科尔·德尔拉·特普瑞玛酒店
List notbigtough=new ArrayList();
//int w=aspectRatio.getWidth();
int w=最大宽度;
//int h=aspectRatio.getHeight();
int h=最大高度;
用于(大小选项:选项){
if(option.getWidth()=TextureView){
足够大。添加(选项);
}否则{
不足够大。添加(选项);
}
}
}
//选择最小的一个,从那些大到小的大到小。
//这是一个非常重要的问题
//阿巴斯坦扎·格兰迪
如果(足够大。大小()>0){
return Collections.min(足够大,新的CompareSizesByArea());
}else if(notbigtough.size()>0){
返回Collections.max(不够大,新的CompareSizesByArea());
}否则{
Log.e(“Camera2”,“找不到任何合适的预览大小”);
返回选项[0];
}
}
预览大小应与用于显示它的曲面的纵横比相匹配。Camera2示例使用AutoFitTextureView包装类,该类有助于重塑视图,使其符合预览大小,但将其复制到项目中很容易出错,因为这取决于充气机的细微差别
为了保持简单,请将TextureView的大小设置为1600px x x 900px,然后
将此值。previewSize
设置为1920 x 1080。您应该看看谷歌官方的Android Camera2示例:
具体来说,您要实现的主要功能是通过Camera2BasicFragment文件中的和完成的,下面是一个片段:
private 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 Size chooseOptimalSize(Size[]选项,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];
}
}
开始使用Android Camera2 API可能会让人望而生畏。除了上述的官方样本,还有一些(希望如此)有用的博客帖子,如: