gpt4 book ai didi

Android:使用 setTransform 翻转实时 TextureView

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:19 27 4
gpt4 key购买 nike

主 Activity

public class MainActivity  extends Activity implements TextureView.SurfaceTextureListener{
private Camera mCamera;
private TextureView mTextureView;

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

mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);

setContentView(mTextureView);
}

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

try {
mCamera.setPreviewTexture(surface);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
}

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

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
int arg2) {
// TODO Auto-generated method stub

}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
// TODO Auto-generated method stub
}
}

实时预览翻转到左侧。在搜索时我发现 setTransform 是一个解决方案,但我不确定如何使用它。谁能给我一个代码示例?

注意:是翻转实时预览,不是图片。

最佳答案

下面是一个使用 SurfaceTexture 显示前置摄像头而不进行镜像的最小示例。请注意,为简洁起见,错误检查大部分都被删除了。此外,在此示例中,我没有遵循在后台线程中打开相机的建议(在某些设备上,这可能会卡住 UI 的时间过长)。

import java.io.IOException;

import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Surface;
import android.view.TextureView;
import android.widget.FrameLayout;

public class SurfaceTextureActivity extends Activity implements TextureView.SurfaceTextureListener{

private Camera mCamera;
private TextureView mTextureView;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);

setContentView(mTextureView);
}

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

int cameraId = 0;
Camera.CameraInfo info = new Camera.CameraInfo();

for (cameraId = 0; cameraId < Camera.getNumberOfCameras(); cameraId++) {
Camera.getCameraInfo(1, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
break;
}

mCamera = Camera.open(cameraId);
Matrix transform = new Matrix();

Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
int rotation = getWindowManager().getDefaultDisplay()
.getRotation();

Log.i("onSurfaceTextureAvailable", " CameraOrientation(" + cameraId + ")" + info.orientation + " " + previewSize.width + "x" + previewSize.height + " Rotation=" + rotation);

switch (rotation) {
case Surface.ROTATION_0:
mCamera.setDisplayOrientation(90);
mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.height, previewSize.width, Gravity.CENTER));
transform.setScale(-1, 1, previewSize.height/2, 0);
break;

case Surface.ROTATION_90:
mCamera.setDisplayOrientation(0);
mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.width, previewSize.height, Gravity.CENTER));
transform.setScale(-1, 1, previewSize.width/2, 0);
break;

case Surface.ROTATION_180:
mCamera.setDisplayOrientation(270);
mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.height, previewSize.width, Gravity.CENTER));
transform.setScale(-1, 1, previewSize.height/2, 0);
break;

case Surface.ROTATION_270:
mCamera.setDisplayOrientation(180);
mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.width, previewSize.height, Gravity.CENTER));
transform.setScale(-1, 1, previewSize.width/2, 0);
break;
}


try {
mCamera.setPreviewTexture(surface);
} catch (IOException t) {
}

mTextureView.setTransform(transform);
Log.i("onSurfaceTextureAvailable", "Transform: " + transform.toString());

mCamera.startPreview();

}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Ignored, the Camera does all the work for us
}

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

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Update your view here!
}
}

关于Android:使用 setTransform 翻转实时 TextureView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20878232/

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