gpt4 book ai didi

android - FrameLayout 正在缩小 ImageView ,但相机在 FrameLayout 内正确打开

转载 作者:行者123 更新时间:2023-11-29 16:05:14 25 4
gpt4 key购买 nike

我在横向模式下的 FrameLayout 中打开 android Camera,然后屏幕看起来像-

enter image description here

但是当我在 FrameLayout 中的 ImageView 中打开相同的图像时,它看起来像 - enter image description here

请忽略图片内容和第二幅画的搞笑方 block 。不同的是,第一个 CameraView 在整个屏幕或高度中打开,触及屏幕的头部,但第二个图像收缩图像以适合。

我希望第一张图片也能像第二张一样工作,它应该适合定义的矩形。唯一的目标是 Camera View 和 ImageView 显示相同的 Look。

如果需要,我也可以更改整个布局。FacePreviewImageView 是我添加的图像。

第一个相机 View 仅是 Android 相机,我正在添加到 android framelayout-

Camera mCamera = Camera.open(1);
FrameLayout layout = (FrameLayout) findViewById(R.id.ll2);
layout.addView(new CameraView());

我添加的第二帧就像-

layout.removeAllViews();
layout.addView(faceImageView);
faceView.setVisibility(View.GONE);
faceImageView.setVisibility(View.VISIBLE);

布局 XML 是 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">

<LinearLayout
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">


</LinearLayout>

<FrameLayout
android:id="@+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal" >

<com.example.defaultfacetracker.FacePreviewImageView
android:id="@+id/facePreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@android:drawable/toast_frame"
/>

</FrameLayout>

<LinearLayout
android:id="@+id/ll3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:text="Process" />

</LinearLayout>

</LinearLayout>

我的 SurfaceHolder 看起来像-

class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Camera.PreviewCallback previewCallback;

Preview(Context context, Camera.PreviewCallback previewCallback, Camera mCamera2) {
super(context);
this.previewCallback = previewCallback;
this.mCamera = mCamera2;

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
// mCamera = Camera.open(1);
//mCamera.setDisplayOrientation(270);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}


private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;

Size optimalSize = null;
double minDiff = Double.MAX_VALUE;

int targetHeight = h;

// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}

// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();

List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, w, h);
parameters.setPreviewSize(optimalSize.width, optimalSize.height);

mCamera.setParameters(parameters);
if (previewCallback != null) {
mCamera.setPreviewCallbackWithBuffer(previewCallback);
Camera.Size size = parameters.getPreviewSize();
byte[] data = new byte[size.width*size.height*
ImageFormat.getBitsPerPixel(parameters.getPreviewFormat())/8];
mCamera.addCallbackBuffer(data);
}
// if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// mCamera.setDisplayOrientation(90);
//
// } else {
// mCamera.setDisplayOrientation(0);
// }
mCamera.startPreview();
}

}

最佳答案

您设置的相机预览尺寸的高度和宽度错误,还需要使用setDisplayOrientation,请在您设置高度和宽度的地方显示代码,并设置显示方向度数。

您还可以看到正确初始化相机 here

关于android - FrameLayout 正在缩小 ImageView ,但相机在 FrameLayout 内正确打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19448107/

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