gpt4 book ai didi

方形屏幕上的 Android Camera Preview 已拉伸(stretch)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:36:20 26 4
gpt4 key购买 nike

我正在为 Android 构建一个应用程序,但该设备确实有一个方形屏幕。屏幕为 320x320,相机应用使用 SurfaceView 显示预览,如下所示:

    mCameraView = (SurfaceView) findViewById(R.id.surface);
LayoutParams params = mCameraView.getLayoutParams();
int camera_dimension = getResources().getInteger(R.integer.camera_dimension);
params.height = camera_dimension; //320px
params.width = camera_dimension;

mCameraView.setLayoutParams(params);
mCameraViewHolder = mCameraView.getHolder();
mCameraViewHolder.addCallback(this);
mCameraViewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

在 Surface 上更改,我正在这样做并且它有效但支持者预览是 w480 x h320

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

int mWidth = w;
int mHeight = h;
int mFormat = format;

try {
mCameraAccess.mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
Camera.Parameters param = mCameraAccess.mCamera.getParameters();

List<Camera.Size> SupportedPreview = param.getSupportedPreviewSizes();
int ScreenSize = getResources().getInteger(R.integer.camera_dimension);

for (int i = 0; i < SupportedPreview.size(); i++) {
if (SupportedPreview.get(i).height == ScreenSize) {
param.setPreviewSize(SupportedPreview.get(i).width, SupportedPreview.get(i).height);
break;
}
}
mCameraAccess.mCamera.setParameters(param);
mCameraAccess.mCamera.startPreview();
}

如何确保我在取景器内的预览未被压缩,而是一种中心裁剪。由于预览是一个矩形,我只需要以图像为中心的正方形。通常我使用的是 scaleType,但它在 Surface View 中不受支持

有什么想法吗?

最佳答案

我想到的解决方案是:

1)-Keep surface view full screen,so that is doesn't stretch.

2)-Put a view over surfaceview with full opacity.So that is looks like camera is already squared.

3)-After capturing image or video,you will have to crop them.

For video you have to use some video processing library like javacv.Using this library you can extract video frames,convert them to bitmap,crop bitmap in square and then re-encode into video.

To get accurate results you will need to play around with different techniques like zooming camera during capture etc. according to your needs.

原始图片:

enter image description here

方形图像:

enter image description here

关于方形屏幕上的 Android Camera Preview 已拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34176270/

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