Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Using setTransform for flipping the live TextureView

MainActivity

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
    }
}

The live preview is flipped to left side. On searching I found that setTransform is a solution, but I am not sure how to use it. Can anyone give me a code example?

NB: It is to flip the live preview not an image.

like image 722
Aswathy Avatar asked Oct 22 '25 00:10

Aswathy


1 Answers

Follows a minimal example that uses SurfaceTexture to display front facing camera without mirroring. Note that error checking is mostly dropped for brevity. Also, in this example I do not follow the recommendation to open camera in a background thread (on some devices this may freeze the UI for too long).

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!
}
}
like image 94
Alex Cohn Avatar answered Oct 24 '25 15:10

Alex Cohn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!