主要活动 public class MainActivity extends Activity implements TextureView.SurfaceTextureListener{ private Camera mCamera; private TextureView mTextureView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(s
          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!
}
}
        
             