react utif tif格式图片预览

1、npm install @types/utif --save-dev

2、import { decode, decodeImage,toRGBA8} from 'utif';

3、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
export const GetFile = (url: string, id: string, type?: number) => {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    url += '?id=' + id + '';
    xhr.open('GET', url, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = (e) => {
      if (xhr.status === 200) {
        try {
          let imgInfo = decode(xhr.response)[0];
          decodeImage(xhr.response,imgInfo);
          let imgToRGBA8 = toRGBA8(imgInfo);
          var canvas = document.createElement('canvas');
          var context = canvas.getContext('2d');
          canvas.width = imgInfo.width;
          canvas.height =imgInfo.height;
          var imageData = context.createImageData(imgInfo.width, imgInfo.height);
          imageData.data.set(imgToRGBA8);
          context.putImageData(imageData, 0, 0);
          let result=canvas.toDataURL("image/png");
          resolve(result);
 
        } catch (error) {
          reject(new Error(error));
        }
      } else {
        reject(new Error('图片获取失败'));
      }
    };
    xhr.send();
  }).catch((e) => {
    return e;
  });
};

  

posted @   燕林  阅读(339)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示