NativeScript Fail to connect to camera service

牧云@^-^@ 提交于 2020-01-25 08:27:09

问题


I'am trying to get access native Api to hardware on NativeScript "i don't use no plugins on my code" . when i fire the function startup(0) or startup(1) up to the camera facing chosed i have this errors .

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


}

this is the Errors Log .

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) 

I hope someone shows me what i made wrong on my code . advanced thanks.


回答1:


due to rare tutorials about how to preview camera using NativeScript the error is that there is no textureView Implemented in the Html file

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");
    }
});

and the html file

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

i made a little project if anyone is facing same problem can takes an idea from my repository click here to go project page



来源:https://stackoverflow.com/questions/50785587/nativescript-fail-to-connect-to-camera-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!