Android:使用setTransform翻转实时TextureView

Android:使用setTransform翻转实时TextureView,android,android-camera,Android,Android Camera,main活动 public class MainActivity extends Activity implements TextureView.SurfaceTextureListener{ private Camera mCamera; private TextureView mTextureView; @Override protected void onCreate(Bundle savedInstanceState) { super.

main活动

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显示前向摄像头而不进行镜像的简单示例。请注意,为了简洁起见,大多数情况下会放弃错误检查。此外,在本例中,我没有按照建议在后台线程中打开camera(在某些设备上,这可能会使UI冻结太久)

import java.io.IOException;
导入android.app.Activity;
导入android.graphics.Matrix;
导入android.graphics.SurfaceTexture;
导入android.hardware.Camera;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Gravity;
导入android.view.Surface;
导入android.view.TextureView;
导入android.widget.FrameLayout;
公共类SurfaceTextExtends活动实现TextureView.SurfaceTextRelister{
私人摄像机麦卡梅拉;
私有纹理视图mTextureView;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mTextureView=new TextureView(此);
mTextureView.setSurfaceTextureListener(此);
setContentView(mTextureView);
}
@凌驾
SurfaceTextureAvailable上的公共空心(SurfaceTexture曲面、整型宽度、整型高度){
int=0;
Camera.CameraInfo info=新建Camera.CameraInfo();
对于(cameraId=0;cameraId
感谢您的支持…但我无法运行此程序,因为我运行此程序时程序已崩溃。您是否运行了此程序?我认为mCamera=Camera.open(cameraId);正在获取错误,因为它要求使用姜饼制作Targetapi,但TextureView.SurfacetTextureListener要求使用冰淇淋三明治制作Targetapi。。您正在测试哪种设备上的解决方案?什么系统级别?我用的是4.2.2它是一个平板电脑。安卓版本4.0.4是因为版本的变化吗?奇怪…请发布崩溃日志。也许你的平板电脑没有后置摄像头,比如Nexus7?
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!
}
}