Trying to utilize the native camera API with NativeScript without any plugins has presented an error when attempting to use the takePicture function:
app/shared/camera/camera.service.ts(23,39): error TS2554: Expected 1 argument, but received 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 instantiation
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 = [];
//start 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('startup 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()});
}
//list all cameras available 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.release();
this.camera=null;
}
}
}