Camera 相机实践(基于UTS组件插件)

2023-12-21  本文已影响0人  空腹无才

一、SurfaceView容器

碰到的问题:无法基于容器的镜像翻转

1.1、创建容器对象(index.vue 文件中)

NVLoad() : LinearLayout {
    //必须实现(必须创建一个容器)
    let contentLayout = new LinearLayout(this.$androidContext);
    contentLayout.setOrientation(LinearLayout.VERTICAL);

    // 创建按钮对象
    this.getCamera().forEach((button) => {
        contentLayout.addView(button as Button);
    })
    
    // 创建surfaceView容器
    surfaceView = new SurfaceView(this.$androidContext);
    let layoutParams = ViewGroup.LayoutParams(
        500,
        600
    )
    
    contentLayout.addView(textureView, layoutParams)
    
    return contentLayout;
}


methods: {
    getCamera() : Button[] {
        const numberOfCameras = Camera.getNumberOfCameras(); // 获取摄像头个数
        const cameraList : Button[] = [];
        for (let i = 0; i < numberOfCameras; i++) { // 从创建对应的按钮
            let button = new Button(this.$androidContext)
            button.setText(`摄像头${i}`);
            button.setTag("centerButton");
            button.setOnClickListener(new ButtonClickListsner(i));
            cameraList.push(button);
        };
        
        return cameraList;
    }
}

1.2、创建点击事件

class ButtonClickListsner extends View.OnClickListener {
        cameraId = 0
        constructor(id : number) {
            super();
            this.cameraId = id;
        }
        override onClick(v ?: View) {
            console.log(this.cameraId)
            openCamera(this.cameraId)
        }
}

1.3、打开对应摄像头


function openCamera(id: any) {
    if (camera !== null) {
        (camera as Camera).stopPreview();
        (camera as Camera).release();  // 释放摄像头
        camera = null;
    }
    
    try {
        camera = Camera.open(id as Int);
        (camera as Camera).setPreviewDisplay((surfaceView as SurfaceView).getHolder());
        (camera as Camera).setDisplayOrientation(0); // 设置旋转角度 0 90 180 270
        (camera as Camera).startPreview(); // 启动预览
    } catch (e) {
            console.error(e)
    }
}

1.4、完整代码

<template>
    <view class="defaultStyle"></view>
</template>

<script lang="uts">
    import View from 'android.view.View'
    import Button from 'android.widget.Button'
    import LinearLayout from 'android.widget.LinearLayout';
    import Camera from "android.hardware.Camera";
    import SurfaceView from 'android.view.SurfaceView';
    import ViewGroup from "android.view.ViewGroup";
    import Surface from "android.view.Surface";
    
    let surfaceView : SurfaceView | null = null;
    let camera : Camera | null = null;
    
    class ButtonClickListsner extends View.OnClickListener {
        cameraId = 0
        constructor(id : number) {
            super();
            this.cameraId = id;
        }
        override onClick(v ?: View) {
            console.log(this.cameraId)
            openCamera(this.cameraId)
        }
    }
    
    function openCamera(id: any) {
        if (camera !== null) {
            (camera as Camera).stopPreview();
            (camera as Camera).release();  // 释放摄像头
            camera = null;
        }
    
        try {
            camera = Camera.open(id as Int);
            (camera as Camera).setPreviewDisplay((surfaceView as SurfaceView).getHolder());
            (camera as Camera).setDisplayOrientation(0); // 设置旋转角度 0 90 180 270
            (camera as Camera).startPreview(); // 启动预览
        } catch (e) {
            console.error(e)
        }
    }
    
    export default {
        name: "camera-temps1",
        data() {
            return {}
        },
        
        NVLoad() : LinearLayout {
            //必须实现(必须创建一个容器)
            let contentLayout = new LinearLayout(this.$androidContext);
            contentLayout.setOrientation(LinearLayout.VERTICAL);

            // 创建按钮对象
            this.getCamera().forEach((button) => {
                contentLayout.addView(button as Button);
            })
    
            // 创建surfaceView容器
            surfaceView = new SurfaceView(this.$androidContext);
            let layoutParams = ViewGroup.LayoutParams(
                500,
                600
            )
    
            contentLayout.addView(textureView, layoutParams)
    
            return contentLayout;
        },
        
        methods: {
            getCamera() : Button[] {
                const numberOfCameras = Camera.getNumberOfCameras(); // 获取摄像头个数
                const cameraList : Button[] = [];
                for (let i = 0; i < numberOfCameras; i++) { // 从创建对应的按钮
                    let button = new Button(this.$androidContext)
                    button.setText(`摄像头${i}`);
                    button.setTag("centerButton");
                    button.setOnClickListener(new ButtonClickListsner(i));
                    cameraList.push(button);
                };
        
                return cameraList;
            }
        }
    }
}
</script>

二、TextureView(容器)

<template>
    <view class="defaultStyle">

    </view>
</template>
<script lang="uts">
    import View from 'android.view.View'
    import Button from 'android.widget.Button'
    import LinearLayout from 'android.widget.LinearLayout';
    import Camera from "android.hardware.Camera";
    import SurfaceView from 'android.view.SurfaceView';
    import ViewGroup from "android.view.ViewGroup";
    import TextureView from "android.view.TextureView";


    let camera : Camera | null = null;
    let textureView : TextureView | null = null;

    class PreviewCallback extends Camera.PreviewCallback {
        constructor() { super() }
        override onPreviewFrame(data : ByteArray, camera : Camera) {
            console.log(data)
        }
    }

    class ButtonClickListsner extends View.OnClickListener {
        cameraId = 0
        constructor(id : number) {
            super();
            this.cameraId = id;
        }
        override onClick(v ?: View) {
            console.log(this.cameraId)
            openCamera(this.cameraId)
        }
    }


    function openCamera(id : any) {
        if (camera !== null) {
            (camera as Camera).stopPreview();
            (camera as Camera).release();
            camera = null;
        }

        try {
        
            camera = Camera.open(id as Int);    
            // 设置镜像
            (textureView as TextureView).setRotationY((180 as Number).toFloat());
            (camera as Camera).setPreviewTexture((textureView as TextureView).getSurfaceTexture());
            // 设置旋转方向   
            (camera as Camera).setDisplayOrientation(0);
            (camera as Camera).startPreview();  
        } catch (e) {
            console.error(e)
        }
    }




    export default {
        name: "camera-temps1",
        data() {
            return {}
        },
        NVLoad() : LinearLayout {
            //必须实现  
            let contentLayout = new LinearLayout(this.$androidContext);
            contentLayout.setOrientation(LinearLayout.VERTICAL);
            
            // 添加按钮
            this.getCamera().forEach((button) => {
                contentLayout.addView(button as Button);
            })
            
            // TextureView 中this.$androidContext 必须加 !号
            textureView = new TextureView(this.$androidContext!);
            
            let layoutParams = ViewGroup.LayoutParams(
                500,
                600
            );
            contentLayout.addView(textureView, layoutParams);
            
            return contentLayout;
        },

        methods: {          
            getCamera() : Button[] {
                const numberOfCameras = Camera.getNumberOfCameras();
                const cameraList : Button[] = [];
                for (let i = 0; i < numberOfCameras; i++) {
                    let button = new Button(this.$androidContext)
                    button.setText(`摄像头${i}`);
                    button.setTag("centerButton");
                    button.setOnClickListener(new ButtonClickListsner(i))
                    cameraList.push(button)
                }
                
                return cameraList;
            }
        }
    }
</script>

<style>
    .defaultStyle {
        width: 750rpx;
        height: 240rpx;
        background-color: red;
    }
</style>

三、权限请求(index.uts)

import Manifest from "android.Manifest";
import Surface from "android.view.Surface";
import Camera from "android.hardware.Camera";
export function getquanxian() { 
    let permissionNeed = [Manifest.permission.CAMERA];

    UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _ : string[]) {
        console.log("用户同意了权限", allRight)
    }, function (_ : boolean, _ : string[]) {
        console.log("用户拒绝了部分权限:")
    })

}

上一篇下一篇

猜你喜欢

热点阅读