【问题标题】:Ionic Resizing Image from cordova-camera-preview?来自cordova-camera-preview的离子调整图像大小?
【发布时间】:2019-07-19 11:50:13
【问题描述】:

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

我正在使用 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 => {

        });
      }

【问题讨论】:

    标签: angular cordova ionic-framework image-processing


    【解决方案1】:

    插件 (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 编码会增加很多开销)

    【讨论】:

    • 是的,不幸的是,宽度和高度不是用于实际结果,仅用于屏幕预览。所以我需要降低这些尺寸。
    • @dunny 好的,但您是否尝试使用文件选项与 base64? base64 会显着增加文件大小的开销
    猜你喜欢
    • 2023-04-10
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多