Android Camera 摄像头 预览时 水平镜像

Android Camera 摄像头 预览时 水平镜像_第1张图片

一、效果图

  • 废话不多说,直接看效果

Android Camera 摄像头 预览时 水平镜像_第2张图片

Android Camera 摄像头 预览时 水平镜像_第3张图片

二、关键代码

Matrix matrix = surfaceView.getMatrix();
matrix.setScale(-1, 1, surfaceView.getWidth()/2, 0);
surfaceView.setTransform(matrix);
Log.e(TAG, "width="+surfaceView.getWidth());

三、全部代码

activity_main.xml


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextureView
        android:id="@+id/camera_surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>

LinearLayout>

MainActivity.class

package com.example.cameraapplication;

import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    Camera camera;
    TextureView surfaceView;

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

        surfaceView = findViewById(R.id.camera_surface_view);

        surfaceView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {

                try{
                    camera = Camera.open(0);
                    if (camera!=null) {
                        camera.setPreviewTexture(surface);
                        camera.setDisplayOrientation(0);
                        camera.startPreview();
                    }
                    Matrix matrix = surfaceView.getMatrix();
                    matrix.setScale(-1, 1, surfaceView.getWidth()/2, 0);
                    surfaceView.setTransform(matrix);
                    Log.e(TAG, "width="+surfaceView.getWidth());

                }catch (Exception e){
                    e.printStackTrace();
                }
            }

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

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                if (camera != null) {
                    camera.setPreviewCallback(null);
                    camera.stopPreview();
                    camera.release();
                    camera = null;
                }
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

            }
        });
    }

}

觉得好,就一键三连呗(点赞+收藏+关注)

你可能感兴趣的:(Android,android,java,camera,surfaceView,matrix)