科尔多瓦相机预览的离子缩放图像?

时间:2019-07-19 11:50:13

标签: angular cordova ionic-framework image-processing

所以目前我从手机摄像头获得的图像尺寸太大。

我正在使用cordova-camera-preview-plugin,保存文件并使用Ionic Native File将其移动到永久位置。

现在,我正在尝试使用ng2-img-tools调整图像的大小,但是它抱怨图像不是png / jpg。

我认为这是因为ng2-img-tools需要一个文件,而我正在传递图像的位置。那么,有谁知道如何解决这个问题?

任何帮助表示赞赏。

import { File } from '@ionic-native/file/ngx';
import { Ng2ImgToolsService } from 'ng2-img-tools';
import { CameraPreview, CameraPreviewPictureOptions } from '@ionic-native/camera-preview/ngx';

constructor(private file: File, public cameraPreview: CameraPreview, private ng2ImgToolsService: Ng2ImgToolsService) { }

    takePhoto()
      {

        this.cameraPreview.takePicture({quality: .7}).then((path) => {

          let tempPath = path[0].substr(0, path[0].lastIndexOf('/') + 1);
          let tempFileName = path[0].replace(tempPath, ""); 
          let fullTempPath = 'file://' + tempPath;

          var newFilename = this.createFileName();
          var permLocation = this.file.dataDirectory + newFilename;

          // Copy Image to Perm Directory....
          this.file.copyFile(fullTempPath, tempFileName, this.file.dataDirectory, newFilename).then(success => {

            var localLocation = window.Ionic.WebView.convertFileSrc(permLocation);

            this.ng2ImgToolsService.resize([localLocation], 640, 640).subscribe(result => {
                //all good, result is a file
                console.info(result);

            }, error => {
            });

          }, error => {

          });

        }, error => {

        });
      }

1 个答案:

答案 0 :(得分:0)

插件(CameraPreview)可以使用以下选项:

// picture options
const pictureOpts: CameraPreviewPictureOptions = {
  width: 1280,
  height: 1280,
  quality: 85,
  storeToFile: true
}

// take a picture
this.cameraPreview.takePicture(this.pictureOpts).then((filePath) => {
  // here move filePath to the location you need, as it will be stored initially in a temp location.
}, (err) => {
  console.log(err);
  this.picture = 'assets/img/test.jpg';
});

因此,我建议使用选项来降低捕获时的分辨率以及使用功能将图像存储为文件(因为base64编码会增加很多开销)