img-type三方库

2025-01-06 09:02:17
37次阅读
0个评论

img-type

这是一个简单的模块,用于根据图像的幻数检查图像的类型(无需加载整个图像)。 它目前适用于JPEG、PNG、GIF、BMP和ICO。

This is a simple module to check the type of an image from its magic number (without having to load the whole image). It currently works for JPEGs, PNGs, GIFs, BMPs and ICOs.

支持以下格式

注意:JPEG、PNG、GIF、BMP和ICO

使用方法

ohpm install @nutpi/img-type
import { imagetype } from '@nutpi/img-type'

imagetype(pathToImage, (type?: string) => {
  // type can take the following values:
  // 'jpeg', 'png', 'gif', 'bmp', 'ico' or null if the type is not recognized
  console.log('The image type is', type);
});

完整示例

import {detectFileType } from '@nutpi/img-type'
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { image } from '@kit.ImageKit';

@Entry
@Component
struct Index {

  @State getAlbum: string = '打开相册中的图片';
  @State pixel: image.PixelMap | undefined = undefined;
  @State albumPath: string = '';
  @State photoSize: number = 0;

  @State imgPath: string = '';
  @State imgType: string = '';

  async getPictureFromAlbum() {
    // 拉起相册,选择图片
    let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
    PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
    PhotoSelectOptions.maxSelectNumber = 1;
    let photoPicker = new photoAccessHelper.PhotoViewPicker();
    let photoSelectResult: photoAccessHelper.PhotoSelectResult = await photoPicker.select(PhotoSelectOptions);
    this.albumPath = photoSelectResult.photoUris[0];
    console.info('xx albumPath: ' + this.albumPath);


    this.imgPath = '选择图片路径是:' + this.albumPath
    // 获取图片后辍
    detectFileType(this.albumPath, (type?: string) => {
      console.info('xx 图片类型: ' + type);
      this.imgType = '图片类型是:' + type
    })
  }

  build() {
    Column({space: 30}) {
      Text(this.getAlbum)
        .fontSize(20)
        .onClick(() => {
          this.getPictureFromAlbum();
        })

      Text(this.imgPath)
        .fontSize(20)

      Text(this.imgType)
        .fontSize(20)


    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}

官网

https://www.nutpi.net/

收藏00

登录 后评论。没有帐号? 注册 一个。