android - TextureView 的裁剪相机预览

标签 android opengl-es android-camera textureview

我有一个具有固定宽度和高度的 TextureView,我想在其中显示一个相机预览。我需要裁剪相机预览,使其在我的 TextureView 中看起来不会被拉伸(stretch)。如何进行种植?如果我需要使用 OpenGL,如何将 Surface Texture 绑定(bind)到 OpenGL 以及如何使用 OpenGL 进行裁剪?

public class MyActivity extends Activity implements TextureView.SurfaceTextureListener 
{

   private Camera mCamera;
   private TextureView mTextureView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options);

    mTextureView = (TextureView) findViewById(R.id.camera_preview);
    mTextureView.setSurfaceTextureListener(this);
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    mCamera = Camera.open();

    try 
    {
        mCamera.setPreviewTexture(surface);
        mCamera.startPreview();
    } catch (IOException ioe) {
        // Something bad happened
    }
}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    mCamera.stopPreview();
    mCamera.release();
    return true;
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface)
 {
    // Invoked every time there's a new Camera preview frame
 }
}

另外,在正确进行预览后,我需要能够实时读取裁剪图像中心的像素。

最佳答案

@Romanski 提供的早期解决方案效果很好,但它会随着裁剪而缩放。如果您需要缩放以适应,请使用以下解决方案。每次更改表面 View 时调用 updateTextureMatrix:即在 onSurfaceTextureAvailable 和 onSurfaceTextureSizeChanged 方法中。另请注意,此解决方案依赖于 Activity 忽略配置更改(即 android:configChanges="orientation|screenSize|keyboardHidden"或类似的东西):

private void updateTextureMatrix(int width, int height)
{
    boolean isPortrait = false;

    Display display = getWindowManager().getDefaultDisplay();
    if (display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180) isPortrait = true;
    else if (display.getRotation() == Surface.ROTATION_90 || display.getRotation() == Surface.ROTATION_270) isPortrait = false;

    int previewWidth = orgPreviewWidth;
    int previewHeight = orgPreviewHeight;

    if (isPortrait)
    {
        previewWidth = orgPreviewHeight;
        previewHeight = orgPreviewWidth;
    }

    float ratioSurface = (float) width / height;
    float ratioPreview = (float) previewWidth / previewHeight;

    float scaleX;
    float scaleY;

    if (ratioSurface > ratioPreview)
    {
        scaleX = (float) height / previewHeight;
        scaleY = 1;
    }
    else
    {
        scaleX = 1;
        scaleY = (float) width / previewWidth;
    }

    Matrix matrix = new Matrix();

    matrix.setScale(scaleX, scaleY);
    textureView.setTransform(matrix);

    float scaledWidth = width * scaleX;
    float scaledHeight = height * scaleY;

    float dx = (width - scaledWidth) / 2;
    float dy = (height - scaledHeight) / 2;
    textureView.setTranslationX(dx);
    textureView.setTranslationY(dy);
}

您还需要以下字段:

private int orgPreviewWidth;
private int orgPreviewHeight;

在调用updateTextureMatrix之前在onSurfaceTextureAvailable方法中初始化它:

Camera.Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);

Pair<Integer, Integer> size = getMaxSize(parameters.getSupportedPreviewSizes());
parameters.setPreviewSize(size.first, size.second);

orgPreviewWidth = size.first;
orgPreviewHeight = size.second;

camera.setParameters(parameters);

getMaxSize 方法:

private static Pair<Integer, Integer> getMaxSize(List<Camera.Size> list)
{
    int width = 0;
    int height = 0;

    for (Camera.Size size : list) {
        if (size.width * size.height > width * height)
        {
            width = size.width;
            height = size.height;
        }
    }

    return new Pair<Integer, Integer>(width, height);
}

最后一件事 - 您需要更正相机旋转。所以在 Activity 的 onConfigurationChanged 方法中调用 setCameraDisplayOrientation 方法(同时在 onSurfaceTextureAvailable 方法中进行初始调用):

public static void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera)
{
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation)
    {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
    {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    }
    else
    {  // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);

    Camera.Parameters params = camera.getParameters();
    params.setRotation(result);
    camera.setParameters(params);
}

关于android - TextureView 的裁剪相机预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019588/

相关文章:

java - 为 JavaCameraView 使用自定义类会使应用程序崩溃

android - Affdex Android SDK - 保存和使用 CameraDetector

javascript - 在 Android 手机浏览器中播放声音

java - onClickListener 在 setContentView 后不起作用

android渲染使用CPU而不是GPU?

C++ QNetworkAccessManager (Qt) 结合 openGL

java - 通过 java 套接字传输大文件

android - 从 Activity onStart() 调用 Google Play 服务 GoogleApiClient.connect() 导致间歇性 OpenGL 崩溃(NvRmChannelSubmit 错误等)

opengl-es - OpenGL,GL_MODULATE和多重纹理

android - 为什么在设置 MediaRecorder 时显示错误 IllegalStateException?