Fail to connect to camera service

android

#1

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 choosed i have this error .

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.


#2

You do have to install the camera plugin: https://docs.nativescript.org/hardware/camera