NativeScript无法连接到摄像头服务 [英] NativeScript Fail to connect to camera service

查看:85
本文介绍了NativeScript无法连接到摄像头服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Native Api可以访问NativeScript上的硬件"我在代码中不使用任何插件".当我将功能startup(0)或startup(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 chosed i have this errors .

无法连接到相机服务

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


}

这是错误日志.

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.

推荐答案

由于有关如何使用NativeScript预览相机的罕见教程 错误是Html文件中没有实现TextureView

due to rare tutorials about how to preview camera using NativeScript the error is that there is no textureView Implemented in the Html file

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>

如果有人遇到相同的问题,我可以做一个小项目,可以从我的存储库中获取一个想法

i made a little project if anyone is facing same problem can takes an idea from my repository click here to go project page

这篇关于NativeScript无法连接到摄像头服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆