欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS如何扫描HEIF格式的二维码图片

程序员文章站 2023-12-16 08:17:16
一、说在前面 iphone 7 之后系统相机支持heif格式的图片,在扫描二维码时,该格式的图片需要转换成jpg格式才能进行扫描成功。 二、思路 用...

一、说在前面

iphone 7 之后系统相机支持heif格式的图片,在扫描二维码时,该格式的图片需要转换成jpg格式才能进行扫描成功。

二、思路

用户在打开系统相册选择完一张照片后,要判断该照片是否是heif格式,如果是,进行格式转换之后再扫描jpg图片的内容,得到扫描结果。

三、具体实现

因为过程中用到photokit,因此需要加入该头文件,#import

@protocol tvuimagemanagerdelegate <nsobject>
- (void)letqrcodevcscanqrcodeimageinalbum:(bool)isheifformat;
@end

@interface tvuimagemanager : nsobject

@property (strong, nonatomic) uiimage *jpgimage;
@property (strong, nonatomic) uiimage *selectedimage;
@property (strong, nonatomic) phasset *selectedasset;
@property (nonatomic,strong) id <tvuimagemanagerdelegate> delegate;

+ (tvuimagemanager *)defaultmanager;
- (void)dosomethingafterselectedimage:(nsdictionary *)info;

@end

自定义类:(.mm)

#define ios9orlater ([uidevice currentdevice].systemversion.integervalue >= 9)

@implementation tvuimagemanager


#pragma mark - init part
+ (tvuimagemanager *)defaultmanager
{
  static tvuimagemanager *m_manager = nil;
  if (m_manager == nil) {
    m_manager = [[tvuimagemanager alloc] init];
  }
  return m_manager;
}

- (id)init
{
  self = [super init];
  if (self) {

  }
  return self;
}


#pragma mark - image format part
- (void)dosomethingafterselectedimage:(nsdictionary *)info
{
  [self getselectedimagephasset:info];
  if (self.selectedasset == nil) {
    log4cplus_error("tvuimagemanagerlog", "%s:selected image is null",__func__);
    return;
  }else {
    bool isheif = [self judgeifanimageisheifformat];
    if (isheif) {
      log4cplus_error("tvuimagemanagerlog", "%s:selected image is heif format",__func__);
      [self convertimageformatfromheiftojpegandthenscantheimage];
    }else {
      log4cplus_error("tvuimagemanagerlog", "%s:selected image is not heif format",__func__);
      self.selectedimage = (uiimage *)[info objectforkey:uiimagepickercontrolleroriginalimage];
      if (self.selectedimage == nil) {
        log4cplus_error("tvuimagemanagerlog", "%s:selected image is null",__func__);
        return;
      }
      [self.delegate letqrcodevcscanqrcodeimageinalbum:no];
    }
  }
}

// choose a picture from the system album
- (void)getselectedimagephasset:(nsdictionary *)info
{
  log4cplus_error("tvuimagemanagerlog", "%s:get selected image phasset",__func__);
  nsurl *imageasseturl = [info objectforkey:uiimagepickercontrollerreferenceurl];
  phfetchresult *fetchresult = [phasset fetchassetswithalasseturls:[nsarray arraywithobject:imageasseturl] options:nil];
  if (fetchresult.count == 0) {
    log4cplus_error("tvuimagemanagerlog", "%s:fetchresult's count is 0",__func__);
    return;
  }
  self.selectedasset = (phasset*)fetchresult.firstobject;
}

// determine if the picture is in heif format
- (bool)judgeifanimageisheifformat
{
  log4cplus_error("tvuimagemanagerlog", "%s:begin to judge if an image is heif format",__func__);
  __block bool isheif = no;
  if (ios9orlater) {
    nsarray *resourcelist = (nsarray *)[phassetresource assetresourcesforasset:self.selectedasset];
    [resourcelist enumerateobjectsusingblock:^(id _nonnull obj, nsuinteger idx, bool * _nonnull stop) {
      phassetresource *resource = obj;
      nsstring *uti = resource.uniformtypeidentifier;
      if ([uti isequaltostring:@"public.heif"] || [uti isequaltostring:@"public.heic"]) {
        isheif = yes;
        *stop = yes;
      }
    }];
  }else {
    nsstring *uti = [self.selectedasset valueforkey:@"uniformtypeidentifier"];
    isheif = [uti isequaltostring:@"public.heif"] || [uti isequaltostring:@"public.heic"];
  }
  return isheif;
}

// convert image format from heif to jpeg and then scan the image
- (void)convertimageformatfromheiftojpegandthenscantheimage
{
  log4cplus_error("tvuimagemanagerlog", "%s:begin to convert image format from heif to jpeg",__func__);
  [[phimagemanager defaultmanager] requestimagedataforasset:self.selectedasset options:nil resulthandler:^(nsdata * _nullable imagedata, nsstring * _nullable datauti, uiimageorientation orientation, nsdictionary * _nullable info) {
    ciimage *ciimage = [ciimage imagewithdata:imagedata];
    cicontext *context = [cicontext context];
    nsdata *jpgdata = [context jpegrepresentationofimage:ciimage colorspace:ciimage.colorspace options:@{}];
    self.jpgimage = [uiimage imagewithdata:jpgdata];
    [self.delegate letqrcodevcscanqrcodeimageinalbum:yes];
  }];
}

@end

调用:

#pragma mark uiimagepickercontrollerdelegate
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info
{
  [self.imagemgr dosomethingafterselectedimage:info];
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: