NativeScript错误TS2554:预期1个参数,但得到0;

时间:2018-06-08 15:20:15

标签: android angular typescript nativescript angular2-nativescript

我尝试使用NativeScript的相机原生API("我不使用任何插件")当我到达takePicture函数时,它会出现此错误:

  

app / shared / camera / camera.service.ts(23,39):错误TS2554:预期1个参数,但得到0. app / shared / camera / camera.service.ts(23,84):错误TS1005 :','预期

     import { Injectable} from '@angular/core';
     import * as permissions from 'nativescript-permissions';
     @Injectable()

     export class CameraService  {
       Camera:any; //Camera android.hardware.Camera instatiation
       camera:any;
       constructor() {
         let CAMERA = () => (android as any).Manifest.permission.CAMERA;
       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;
        }  
       CamList = [];
       //satrt up the camera
       startup(cameraID:number){
try{
         this.Camera.open(cameraID);
         this.Camera.startPreview();
         this.Camera.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){
let bitmap = android.graphics.BitmapFactory.decodeByteArray(data,0,data.length);
let outputStream = new java.io.ByteArrayOutputStream();
bitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, outputStream);
let img=[];
img.push({image:true,buffer:outputStream.toByteArray()});

      }
      //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(this.Camera.getCameraInfo(i,camera));
}
//console.dir(this.CamList);

return this.CamList ;
      }
       //release camera
    releasecamera(){
if(this.camera != null ){
  this.camera.stopPreview();
  this.camera.realase();
  this.camera=null;
}
       }


     }

1 个答案:

答案 0 :(得分:2)

这个怎么样?

        this.Camera.takePicture(null, null, new android.hardware.Camera.PictureCallback({
            onPictureTaken: async (data, camera) => {
                this.releasecamera();
                this.sendpicture(data);
            }
        }));

请不要犹豫,查看有关扩展接口documentation about extending interfaces

的文档