本机脚本无法连接到相机服务



我正在尝试在NativeScript上访问硬件的本机API"我不在我的代码上使用任何插件" . 当我触发函数 startup(0( 或 startup(1( 直到所选的相机时,我遇到了此错误。

无法连接到相机服务

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

}

这是 错误日志 .

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) 

我希望有人向我展示我在代码上做错了什么。 高级谢谢。

由于关于如何使用 NativeScript 预览相机的教程很少 错误是 Html 文件中没有实现纹理视图

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

和 HTML 文件

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

我做了一个小项目,如果有人遇到同样的问题,可以从我的存储库中获取一个想法,请单击此处转到项目页面

最新更新