即时通讯开发
即时通讯开发
  • 发布:2024-01-24 12:09
  • 更新:2024-01-24 17:27
  • 阅读:439

uts调用原生摄像头,总是报错,请教一下

分类:uts
import CameraManager from "android.hardware.camera2.CameraManager";  
import Context from "android.content.Context"  
const context = UTSAndroid.getUniActivity()!;  
const cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager  
cameraManager.openCamera('0')

打印 cameraManager 是有opencamera 方法,但是调用的时候就报找不到这个方法

2024-01-24 12:09 负责人:无 分享
已邀请:
DCloud_Android_DQQ

DCloud_Android_DQQ

发一下最简示例吧。
我帮你看一下

  • 即时通讯开发 (作者)

    楼下我放了附件,您帮忙看看谢谢

    2024-01-24 12:50

即时通讯开发

即时通讯开发 (作者)

简单示例 我可能不太了解原生,有的东西找不到地方,您看看写的对不对

DCloud_Android_DQQ

DCloud_Android_DQQ

写法有问题,修改后的关键代码 我复制在下面了。这样测试可以运行


import { MyApiOptions, MyApiResult, MyApi, MyApiSync } from '../interface.uts';  

import { MyApiFailImpl } from '../unierror';  
import CameraManager from "android.hardware.camera2.CameraManager";  
import Build from 'android.os.Build';  
import Context from "android.content.Context"  
import CameraDevice from "android.hardware.camera2.CameraDevice"  
import Handler from "android.os.Handler"  
import Looper from "android.os.Looper"  
import Manifest from "android.Manifest";  
// 引用android api  
import BatteryManager from "android.os.BatteryManager";// 引用uts环境库  

export function getBatteryCapacity() : string {  
    // 获取android系统 application上下文  
    const context = UTSAndroid.getUniActivity()!;  
        const manager = context.getSystemService(  
            Context.BATTERY_SERVICE  
        ) as BatteryManager;  
        const currentLevel : number = manager.getIntProperty(  
            BatteryManager.BATTERY_PROPERTY_CAPACITY  
        );  
        console.log(currentLevel)  
        return '' + currentLevel + '%';  
}  

const cameraManager = UTSAndroid.getUniActivity()!.getSystemService(Context.CAMERA_SERVICE) as CameraManager  
export const myApi : MyApi = function (options : MyApiOptions) {  
    if (options.paramA == true) {  
        // 返回数据  
        const res : MyApiResult = {  
            fieldA: 85,  
            fieldB: true,  
            fieldC: 'some message'  
        };  
        options.success?.(res);  
        options.complete?.(res);  
    } else {  
        // 返回错误  
        const err = new MyApiFailImpl(9010001);  
        options.fail?.(err)  
        options.complete?.(err)  
    }  
}  

export const myApiSync : MyApiSync = function (paramA : boolean) : MyApiResult {  
    // 返回数据,根据插件功能获取实际的返回值  
    const res : MyApiResult = {  
        fieldA: 85,  
        fieldB: paramA,  
        fieldC: 'some message'  
    };  
    return res;  
}  

export function getModelNum() {  
    console.log(Build.MODEL)  
}  

class CustomCallback extends CameraDevice.StateCallback{  
    constructor (){  
        super();  
    }  

    override onOpened(camera: CameraDevice):void {  
        console.log("onOpened")  
    }  

    override onDisconnected(camera: CameraDevice):void {  
        console.log("onDisconnected")  
    }  

    override onError(camera: CameraDevice, error: Int):void {  
        console.log("onError")  
    }  
}  

export function getCameraIds() {  

    let permissionNeed = [Manifest.permission.CAMERA];  
    UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight:boolean,_:string[]) {  
        console.log("用户同意了权限",allRight)  
        const Cameradevice = cameraManager.getCameraCharacteristics('0')  
            console.log(cameraManager)  
        let mainHandler = Handler(Looper.getMainLooper())      
        const aa = cameraManager.openCamera('0', new CustomCallback(), mainHandler)  
    }, function (_:boolean,_:string[]) {  
        console.log("用户拒绝了部分权限:")  
    })  

    // cameraManager.getCameraIdList()  
    // let ca : number = cameraManager.openCamera('0')  
    // console.log(ca)  
}
  • 即时通讯开发 (作者)

    好的 谢谢 我这边试试

    2024-01-25 10:19

  • 3***@qq.com

    回复 即时通讯开发: 能正常调用了吗?可以的话方便发个简单的DEMO不?遇到一样的问题了。。。谢谢啦

    2024-10-18 15:34

  • DCloud_Android_DQQ

    回复 3***@qq.com: 可以直接运行hello uts 里面有完整代码 https://gitcode.net/dcloud/hello-uts/-/blob/master/uni_modules/uts-platform-api/utssdk/app-android/index.uts#L171

    2024-10-21 11:19

要回复问题请先登录注册