NativeScript 无法连接到相机服务

NativeScript Fail to connect to camera service

我正在尝试通过 NativeScript“我不在我的代码中使用任何插件”来访问本机 Api 硬件。当我将功能启动(0)或启动(1)启动到面向选择的相机时,我遇到了这个错误。

Fail to connect to camera service

import { Injectable} from '@angular/core';
import * as SocketIO from "nativescript-socket.io";

import * as permissions from 'nativescript-permissions';
let CAMERA = () => (android as any).Manifest.permission.CAMERA;
@Injectable()

export class CameraService  {
  Camera:any; //Camera android.hardware.Camera instatiation
  camera:any;
  constructor() {

  let RECORD_AUDIO = () => (android as any).Manifest.permission.RECORD_AUDIO;
  let READ_EXTERNAL_STORAGE = () => (android as any).Manifest.permission.READ_EXTERNAL_STORAGE;
  let WRITE_EXTERNAL_STORAGE = () => (android as any).Manifest.permission.WRITE_EXTERNAL_STORAGE;
    this.Camera=android.hardware.Camera; 
    this.camera = android.hardware.Camera;

   }  
  socket = SocketIO.connect('http://localhost:3000');
  CamList = [];
  //satrt up the camera
  startup(cameraID){
    try{
     // this.releasecamera();
    if(!this.hasCameraPermission){ console.log('no permission'); return;}else{console.log('permission granted');}

    let cam = this.Camera.open(cameraID);
    console.log(1); 
    cam.startPreview();
    cam.takePicture(null, null, new android.hardware.Camera.PictureCallback({
            onPictureTaken: async (data, camera) => {
                this.releasecamera();
                this.sendpicture(data);
            }
        }));

    }catch(ex){
      console.log('start up error',ex);
    }
  }
  //send picture
  sendpicture(data){
    try{

    let bitmap = android.graphics.BitmapFactory.decodeByteArray(data,0,data.length);
    console.log('hhere');
    let outputStream = new java.io.ByteArrayOutputStream();
    bitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, outputStream);
    let img=[];
    img.push({image:true,buffer:outputStream.toByteArray()});
   console.log(img);
    console.dir(img);
    this.socket.emit('img',img);}catch(ex){
      console.log('parss prob',ex);
    }

  }
  //liste all cameras avlaible on the device
  getcameras(){
   // let Camera:any = android.hardware.Camera ; 
    let numberOfcams = this.Camera.getNumberOfCameras();  //android.hardware.Camera.getNumberOfCameras();

    for(let i = 0 ; i<numberOfcams;i++){
              let camera = new this.Camera.CameraInfo();
              this.Camera.getCameraInfo(i,camera);
              if(camera.facing == this.Camera.CameraInfo.CAMERA_FACING_FRONT)
                {
                  //let ca = "{name:'front' , id:"+i+"}";
                  this.CamList.push({name:'front',id:i});
                }else if(camera.facing == this.Camera.CameraInfo.CAMERA_FACING_BACK)
                {
                 // let ca = "{name:'back' , id:"+i+"}";
                  this.CamList.push({name:'back',id:i});
                }  else{
                  this.CamList.push({name:'other',id:i});
                }
                console.dir(camera);
    }
    //console.dir(this.CamList);
    //this.releasecamera();
    return this.CamList ;
  }
  public hasCameraPermission(): boolean {
    return permissions.hasPermission(CAMERA());
  }
  //release camera
  releasecamera(){
    if(this.Camera != null ){

      this.Camera.stopPreview();
      this.Camera.release();
      this.Camera = null;
    }
  }


}

这是错误日志。

java.lang.RuntimeException: takePicture failed
JS:     android.hardware.Camera.native_takePicture(Native Method)
JS:     android.hardware.Camera.takePicture(Camera.java:1484)
JS:     android.hardware.Camera.takePicture(Camera.java:1429)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:957)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:941)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:933)
JS:     com.tns.gen.java.lang.Object_frnal_ts_helpers_l58_c38__ClickListenerImpl.onClick(Object_frnal_ts_helpers_l58_c38__ClickListenerImpl.java:12)
JS:     android.view.View.performClick(View.java:5204)
JS:     android.view.View$PerformClick.run(View.java:21052)
JS:     android.os.Handler.handleCallback(Handler.java:739)
JS:     android.os.Handler.dispatchMessage(Handler.java:95)
JS:     android.os.Looper.loop(Looper.java:145)
JS:     android.app.ActivityThread.main(ActivityThread.java:5944)
JS:     java.lang.reflect.Method.invoke(Native Method)
JS:     java.lang.reflect.Method.invoke(Method.java:372)
JS:     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
JS:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 

我希望有人告诉我我的代码有什么错误。 先进的感谢。

由于关于如何使用 NativeScript 预览相机的教程很少 错误是 Html 文件

中没有实现 textureView
public onCreatingView = (args:any)=>{
        if(androidApp){
          var appContext = androidApp.context ;
          this.mtextureview = new android.view.TextureView(androidApp.context);
          this.mtextureview.setSurfaceTextureListener(this.msurfaceTextureLisitiner);
          args.view = this.mtextureview ;

        }if(iosApp){
          console.log("running on ios");
        }
  }
  //the method surfaceTextureListiner callback from the interface
  public msurfaceTextureLisitiner = new android.view.TextureView.SurfaceTextureListener({
    onSurfaceTextureAvailable : (texture,width,height)=>{
      console.log('texture avlaible');
      this.mcamera = android.hardware.Camera.open(this.cid);
      var params:android.hardware.Camera.Parameters = this.mcamera.getParameters();
      this.mcamera.setDisplayOrientation(90);
      params.set("orientation", "portrait");
      this.mcamera.setParameters(params); 
      this.mtextureview = texture;
      try{
          this.mcamera.setPreviewTexture(texture);
          this.mcamera.startPreview();
      }catch(e){
        console.log(e);
      }

    },
    onSurfaceTextureSizeChanged : (texture,width,height)=>{
      console.log('size changed');
    },
    onSurfaceTextureDestroyed : (texture)=>{
         console.log('surface destroyed');
         this.mcamera.stopPreview();
         this.mcamera.release();
         return true;
    },
    onSurfaceTextureUpdated : (texture)=>{
      console.log("texture updated");
    }
});

和 html 文件

<StackLayout orientattion="vertical">
     <Placeholder #surface height="500" *ngIf="init" (creatingView)="onCreatingView($event)" (loaded)="onLoaded(surface)" id="placeholder-view"></Placeholder>
     <Button text="changeCamera" (tap)="cameraId()"></Button>
</StackLayout>

我做了一个小项目,如果有人遇到同样的问题可以从我的存储库中获取想法click here to go project page