简体   繁体   English

使用 Cordova 相机插件选取的图像绘制画布

[英]Drawing canvas with an image picked with Cordova Camera Plugin

I'm getting an image from device and drawing a canvas with filters using Pixi JS.我从设备获取图像并使用 Pixi JS 绘制带有过滤器的画布。 It works all well using computer to get an image.使用计算机获取图像效果很好。 But when I'm on IOS, it throws errors such as cross origin issue, or that I'm trying to use an unknown format.但是当我在 IOS 上时,它会抛出错误,例如跨源问题,或者我正在尝试使用未知格式。

This is the code I'm using to draw the image (that works on web/desktop but not cordova built ios app)这是我用来绘制图像的代码(适用于网络/桌面,但不适用于cordova构建的ios应用程序)

_renderImage() {
    let wWidth;
    if (window.outerWidth > 414) {
      wWidth = 414;
    } else {
      wWidth = window.outerWidth;
    }

    const img = new Image();
    img.src = this.state.avatarSource;

    let lower;
    if (img.width > img.height) {
      lower = img.height;
    } else {
      lower = img.width;
    }

    const canvas = this.refs.canvasimg;
    if (canvas.hasChildNodes()) {
      canvas.removeChild(canvas.childNodes[0]);
    }

    const renderer = PIXI.autoDetectRenderer(wWidth, wWidth * 1.25, {transparent: true});
    const stage = new PIXI.Container();

    canvas.appendChild(renderer.view);

    // create a PIXI sprite from an image path
    const canvasImg = PIXI.Sprite.fromImage(this.state.avatarSource);
    canvasImg.anchor.x = 0;
    canvasImg.anchor.y = 0;
    canvasImg.width = wWidth;
    canvasImg.height = wWidth * 1.25;

    const filter = this._getImageFilter();

    stage.filters = [filter];
    stage.addChild(canvasImg);

    render();

    function render(){
      requestAnimationFrame(render);
      renderer.render(stage);
    }
  }

And this is the code I use to pick the image using cordova camera plugin:这是我用来使用cordova相机插件选择图像的代码:

_getPhoto(isPhotosLib) {
    const captureSuccess = (imageData) => {
      this.m.setState({
        // avatarSource: `data:image/jpeg;base64,${imageData}`
        avatarSource: imageData
      })
    };

    const captureError = (error) => {
      navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error');
    };

    let options = {
      quality: 100,
      targetWidth: 640,
      targetHeight: 640,
      saveToPhotoAlbum: false,
      destinationType: Camera.DestinationType.FILE_URI,
      popoverOptions: new CameraPopoverOptions(640, 640, 100, 100, Camera.PopoverArrowDirection.ARROW_ANY)
    }

    isPhotosLib ? options.sourceType = Camera.PictureSourceType.PHOTOLIBRARY : null;

    navigator.camera.getPicture(captureSuccess, captureError, options);
  }

The error I get is this:我得到的错误是这样的:

Failed to load resource: Origin null is not allowed by Access-Control-Allow-Origin.

And on XCode:在 XCode 上:

[Generic] Creating an image format with an unknown type is an error

And when I changed to NATIVE_URI, it logs this error:当我更改为 NATIVE_URI 时,它会记录此错误:

Failed to load resource: unsupported URL
assets-library://asset/asset.JPG?id=2DDAD0CF-2F82-40A0-B84B-398C996AC749&ext=JPG

So what could be the reason that it doesn't work on ios?那么它在ios上不起作用的原因可能是什么?

Check out this post regarding the Cordova Whitelist Plugin , it has solved most of my issues.查看这篇关于Cordova Whitelist Plugin 的帖子,它解决了我的大部分问题。
I don't know whether or not you are using any WKWebView plugins, but the cordova-wkwebview-engine plugin supports some iOS specific Application Transport Security settings as well https://github.com/apache/cordova-plugin-wkwebview-engine .我不知道您是否在使用任何 WKWebView 插件,但是 cordova-wkwebview-engine 插件也支持一些特定于 iOS 的应用程序传输安全设置https://github.com/apache/cordova-plugin-wkwebview-engine .

Another workaround would be to use an HTML input tag to initiate the camera capture:另一种解决方法是使用 HTML 输入标签来启动相机捕获:

<input type="file" capture="camera" accept="image/*" />

And just listen to the "change" event.只听“更改”事件。

on the option section, set as DATA_URL :选项部分,设置为 DATA_URL :

destinationType: Camera.DestinationType.DATA_URL

this will get image as base64 format, to display the image, add这将得到图像为 base64 格式,要显示图像,添加

<img src="data:image/jpeg;base64,'+ imageData +'">

then the canvas draw from this img tag然后画布从这个img标签中绘制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM