img-type三方库
2025-01-06 09:02:17
87次阅读
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)
}
}
官网
00
- 17回答
- 20粉丝
- 11关注
相关话题
- 给 OpenHarmony 三方库添加徽章
- OpenHarmony三方库使用指南
- 开源第三方库资源汇总
- 常用的ArkTS第三方库
- 最受欢迎的三方库之SpinKit
- 最受欢迎的三方库之harmony-dialog
- 最受欢迎的三方库之harmony-utils
- ArkTS第三方库的语法与使用详解
- 上传PR到第三方库可能遇到的问题
- 【HarmonyOS NEXT】lv-markdown-in 三方库用法说明
- 【HarmonyOS NEXT】lv-markdown-in 三方库用法说明
- 第十四课:HarmonyOS Next第三方库集成指南
- china_area 中国区域数据,省市县三级数据三方库
- FlutterToast 三方库鸿蒙适配之旅:从零到一的深度实践
- 【等待更新】OpenHarmony三方库与原生鸿蒙应用的融合之道(案例集)