NativeScript 错误 TS2554:需要 1 个参数,但得到 0 个;
NativeScript error TS2554: Expected 1 arguments, but got 0;
我尝试使用带有 NativeScript ("i don't use any pluguin") 的原生 API 相机,当我到达 takePicture 函数时出现此错误:
app/shared/camera/camera.service.ts(23,39): error TS2554: Expected 1 arguments, but got 0. app/shared/camera/camera.service.ts(23,84): error TS1005: ',' expected.
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;
}
}
}
这个呢?
this.Camera.takePicture(null, null, new android.hardware.Camera.PictureCallback({
onPictureTaken: async (data, camera) => {
this.releasecamera();
this.sendpicture(data);
}
}));
不要犹豫,查看有关扩展接口的文档documentation about extending interfaces
我尝试使用带有 NativeScript ("i don't use any pluguin") 的原生 API 相机,当我到达 takePicture 函数时出现此错误:
app/shared/camera/camera.service.ts(23,39): error TS2554: Expected 1 arguments, but got 0. app/shared/camera/camera.service.ts(23,84): error TS1005: ',' expected.
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;
}
}
}
这个呢?
this.Camera.takePicture(null, null, new android.hardware.Camera.PictureCallback({
onPictureTaken: async (data, camera) => {
this.releasecamera();
this.sendpicture(data);
}
}));
不要犹豫,查看有关扩展接口的文档documentation about extending interfaces