简体   繁体   English

Angular 10中裁剪后的图像预览问题

[英]Image previewing problem after cropping in Angular 10

in my angular 10 project I have used ngx-image-cropper to crop image.在我的 angular 10 项目中,我使用了 ngx-image-cropper来裁剪图像。 After crop the image ngx-image-cropper gives us base64 string value.裁剪图像后,ngx-image-cropper 为我们提供 base64 字符串值。 But I want to convert this base64 value to file and show this file in another image previewer.但我想将此 base64 值转换为文件并在另一个图像预览器中显示此文件。 Later I want to upload that converted image to server rather saving base64 to database.稍后我想将该转换后的图像上传到服务器,而不是将 base64 保存到数据库。 To convert base64 to image file I have used the below code:要将 base64 转换为图像文件,我使用了以下代码:

convertBase64ToFile(data, filename) {
    const arr = data.split(',');
    const mime = arr[0].match(/:(.*?);/)[1];
    const bstr = atob(arr[1]);
    let n = bstr.length;
    let u8arr = new Uint8Array(n);

    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }

    return new File([u8arr], filename, { type: mime });
  }

Now in imageCropped event, I tried to set that converted image file to a image previewer.现在在imageCropped事件中,我尝试将转换后的图像文件设置为图像预览器。 the code is:代码是:

imageCropped(event: ImageCroppedEvent) {
    this.croppedImage = event.base64;
    
    this.fileToReturn = this.convertBase64ToFile(event.base64, this.imageChangedEvent.target.files[0].name)

    console.log(this.fileToReturn);
    
    var reader = new FileReader();
    reader.onload = (event: any) => {
      this.croppedImage = this.fileToReturn;
    };

    reader.onerror = (event: any) => {
      console.log("File could not be read: " + event.target.error.code);
    };

    reader.readAsDataURL(this.fileToReturn);

    return this.fileToReturn;
  }

If I print this variable fileToReturn value to console I get the below object:如果我将此变量fileToReturn值打印到控制台,我会得到以下 object:

在此处输入图像描述

The HTML: HTML:

<div id="preview" class="text-center col-md-4">
   <h5>Preview</h5>
   <img [src]="croppedImage" />
</div>

But the problem is image is not showing.但问题是图像没有显示。 Instead I get the below message in Console:相反,我在控制台中收到以下消息:

GET http://localhost:4200/[object%20File] 404 (Not Found) GET http://localhost:4200/[object%20File] 404(未找到)

Now can anyone suggest me现在任何人都可以建议我

  1. how to show the converted file to the previewer如何将转换后的文件显示给预览器
  2. how to upload the converted file to server?如何将转换后的文件上传到服务器?

My full code is:我的完整代码是:

<div class="container">
    <div class="row" style="margin-top: 5%;">
        <div class="text-center col-md-12">
            <input type="file" (change)="fileChangeEvent($event)" />
        </div>
    </div>
    <div class="row" style="margin-top: 5%;">
        <div class="text-center col-md-8">
            <h5>Crop Image</h5>
            <image-cropper 
                [imageChangedEvent]="imageChangedEvent" 
                [maintainAspectRatio]="true" 
                [aspectRatio]="4 / 4"
                [resizeToWidth]="256" 
                format="png" 
                (imageCropped)="imageCropped($event)" 
                (imageLoaded)="imageLoaded()"
                (cropperReady)="cropperReady()" 
                (loadImageFailed)="loadImageFailed()">
            </image-cropper>
        </div>
        <div id="preview" class="text-center col-md-4">
            <h5>Preview</h5>
            <img [src]="croppedImage" />
        </div>
    </div>
</div>

TS file: TS 文件:

import { Component, OnInit } from '@angular/core';
import { ImageCroppedEvent } from 'ngx-image-cropper';

@Component({
  selector: 'app-image-cropper1',
  templateUrl: './image-cropper1.component.html',
  styleUrls: ['./image-cropper1.component.css']
})
export class ImageCropper1Component implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

  imageChangedEvent: any = '';
  croppedImage: any = '';
  fileToReturn: any;

  fileChangeEvent(event: any): void {
    this.imageChangedEvent = event;
    //console.log(event);

  }

  imageCropped(event: ImageCroppedEvent) {
    this.croppedImage = event.base64;
    this.fileToReturn = this.convertBase64ToFile(event.base64, this.imageChangedEvent.target.files[0].name)

    //console.log(this.imageChangedEvent.target.files[0]);
    console.log(this.fileToReturn);
    
    var reader = new FileReader();
    reader.onload = (event: any) => {
      this.croppedImage = this.fileToReturn;
    };

    reader.onerror = (event: any) => {
      console.log("File could not be read: " + event.target.error.code);
    };

    reader.readAsDataURL(this.fileToReturn);

    return this.fileToReturn;
  }

  imageLoaded() {
    // show cropper
  }

  cropperReady() {
    // cropper ready
  }

  loadImageFailed() {
    // show message
  }

  convertBase64ToFile(data, filename) {
    const arr = data.split(',');
    const mime = arr[0].match(/:(.*?);/)[1];
    const bstr = atob(arr[1]);
    let n = bstr.length;
    let u8arr = new Uint8Array(n);

    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }

    return new File([u8arr], filename, { type: mime });
  }
}

To convert cropped image as a file instead of a base64 string ngx-image-cropper have base64ToFile method要将裁剪的图像转换为文件而不是 base64 字符串ngx-image-cropper cropper 具有base64ToFile方法

imageCropped(event: ImageCroppedEvent) {
    this.croppedImage = event.base64;
    let File = base64ToFile(this.croppedImage);
}

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

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