【问题标题】:How to get photo from Cordova Camera Preview Plugin?如何从 Cordova 相机预览插件获取照片?
【发布时间】:2017-06-02 20:07:27
【问题描述】:

我正在使用这个插件 Cordova Camera Preview Plugin

当我拍照时,我得到了一个错误。我不知道如何从这个 URL 读取图像。我想要那个图像的base64。

这是错误图片:

这是我的 HTML

<div class="controls">
  <div class="block">
    <button id="startCameraButton">Start Camera at back</button>
    <button id="stopCameraButton">Stop Camera</button>
  </div>

  <div class="block">
    <p><button id="startCameraAnotherPosButton">Start Camera at front</button></p>

    <p>Color Effect:
    <select id="colorEffectCombo">
      <option selected value="none">None</option>
      <option value="aqua">Aqua</option>
      <option value="blackboard">Blackboard</option>
      <option value="mono">Mono</option>
      <option value="negative">Negative</option>
      <option value="posterize">Posterize</option>
      <option value="sepia">Sepia</option>
      <option value="solarize">Solarize</option>
      <option value="whiteboard">Whiteboard</option>
    </select>
    </p>
  </div>
  <div class="block">
    <button id="takePictureButton">Take Picture</button>
    <button id="switchCameraButton">Switch Camera</button>
  </div>
  <div class="block">
    <button id="hideButton">Hide</button>
    <button id="showButton">Show</button>
  </div>
</div>
<div class="pictures">
  <p><img id="previewPicture" width="200"/></p>
  <p><img id="originalPicture" width="200"/></p>
</div>

这是我的 app.js

var app = {
  startCamera: function(){
    console.log('starting camera');
    // var tapEnabled = true; //enable tap take picture
    var dragEnabled = true; //enable preview box drag across the screen
    // var toBack = true; //send preview box to the back of the webview
    var rect = {x: 100, y: 100, width: 300, height:300};
    cordova.plugins.camerapreview.startCamera(rect, "front", dragEnabled)
  },
  stopCamera: function(){
    cordova.plugins.camerapreview.stopCamera();
  },

  startCameraAnotherPos: function(){
    cordova.plugins.camerapreview.startCamera({x: 50, y: 100, width: 300, height:300, camera: "back", tapPhoto: true, previewDrag: true, toBack: false});
  },

  takePicture: function(){
    console.log('taking picture..');
    cordova.plugins.camerapreview.takePicture({maxWidth: 640, maxHeight: 640});
  },

  takepicturehandler: function(){
    console.log('taking..');
  },

  switchCamera: function(){
    cordova.plugins.camerapreview.switchCamera();
  },

  show: function(){
    cordova.plugins.camerapreview.show();
  },

  hide: function(){
    cordova.plugins.camerapreview.hide();
  },

  colorEffectChanged: function(){
    var effect = document.getElementById('colorEffectCombo').value;
    cordova.plugins.camerapreview.setColorEffect(effect);
  },


  init: function(){
    document.getElementById('startCameraButton').addEventListener('click', this.startCamera, false);
    document.getElementById('startCameraAnotherPosButton').addEventListener('click', this.startCameraAnotherPos, false);

    document.getElementById('stopCameraButton').addEventListener('click', this.stopCamera, false);
    document.getElementById('takePictureButton').addEventListener('click', this.takePicture, false);
    document.getElementById('switchCameraButton').addEventListener('click', this.switchCamera, false);
    document.getElementById('showButton').addEventListener('click', this.show, false);
    document.getElementById('hideButton').addEventListener('click', this.hide, false);
    document.getElementById('colorEffectCombo').addEventListener('change', this.colorEffectChanged, false);

    cordova.plugins.camerapreview.setOnPictureTakenHandler(function(result){
      console.log(result);
      document.getElementById('originalPicture').src = result[0];//originalPicturePath;
      document.getElementById('previewPicture').src = result[1];//previewPicturePath;
  });

  }
};

document.addEventListener('deviceready', function(){  
  app.init();
}, false);

【问题讨论】:

  • 我实际上需要同样的东西,但无法让插件运行。你是如何让它运行的,你正在测试的 Android 版本是什么。在 6.0 这个插件有一个权限错误,它对我来说没用。看到这个github.com/cordova-plugin-camera-preview/…
  • 好吧,我实际上修复了插件本身和 java 文件,所以现在它可以在 android 6.0 上运行。明天会尝试使用它,如果我有什么进展,我会发布答案
  • 我在 Android 7.0 上使用这个插件。好吧,我现在终于可以拍照了。我直接从返回的 url 读取文件并将其转换为 base64。但是画质太差了。请如果您发现和解决方案。让我知道@Marko Check this
  • 那么,您不必编辑 .java 文件并安装 cordova 插件 compat 即可在手机上实际启动此插件?因为我不得不,因为运行时的权限。无论如何,您现在可以使用您编写的代码编辑您的问题吗?

标签: cordova ionic-framework camera cordova-plugins


【解决方案1】:

为了使图片可用,需要以正确的格式和正确的路径临时“存储”它。下面是一个基于 Promise 的函数示例:

公共base64Image:任何; 公开信息:任意;

takePicture() {
    this.cameraPreview.takePicture({
      quality: 50
    }).then((imageData) => {
      this.base64Image = 'data:image/jpeg;base64,' + imageData;
    }, (err) => {
      this.message = 'Problem accessing the camera ' + err;
    });
  }

这里,base64Image 是你的图片的路径,你可以在 img 标签中使用它:

<img src="{{base64Image}}">

【讨论】:

    猜你喜欢
    • 2014-06-05
    • 2016-05-11
    • 2015-07-17
    • 1970-01-01
    • 2013-11-23
    • 2011-11-26
    • 2021-07-08
    • 2015-02-06
    相关资源
    最近更新 更多