读取heic格式图像

读取heic格式图像主要通过第三方库实现,首先创建读取的入口,在前面几期中讲过增加“打开”菜单按钮实现功能。

增加“打开图片”菜单:

 QMenu *pMenu = menuBar()->addMenu(QStringLiteral("文件(&F)"));QAction *pOpenFile = new QAction(QStringLiteral("打开图片(&O)"));connect(pOpenFile, &QAction::triggered, this, &heicExample::OpenFile);QAction *pExit = new QAction(QStringLiteral("退出(&Q)"));pMenu->addAction(pOpenFile);pMenu->addAction(pExit);

实现槽函数OpenFile:

void heicExample::OpenFile()
{QString fileName = QFileDialog::getOpenFileName(this, tr("open image file"), "./", tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm *.heic);;All files (*.*)"));  if (pixItem && fileName != ""){m_graphicsScene->removeItem(pixItem);   //将上一个图元从场景中移除,重新添加新的图元delete pixItem;pixItem = NULL;for (int i = 0; i < m_loldrgb.length(); i++){delete m_loldrgb[i];m_loldrgb[i] = NULL;}m_loldrgb.clear();}if(fileName.contains(".heic")){ReadHeif(fileName);}
}

读取heic格式文件:

void heicExample::ReadHeif(QString filename)
{filename = QDir::fromNativeSeparators(filename);Array<ImageId> itemIds;const char* outputFileName = "./abc";// Make an instance of Heif Readerauto *reader = Reader::Create();if (reader->initialize(filename.toLatin1().data()) != ErrorCode::OK)return;// Verify that the file is HEIF format.FileInformation fileInfo;if (reader->getFileInformation(fileInfo) != ErrorCode::OK) {cout << "Unable to get MetaBox! Wrong heif format." << endl;return;}if (!(fileInfo.features & FileFeatureEnum::HasSingleImage ||fileInfo.features & FileFeatureEnum::HasImageCollection)) {cout << "The file don't have images in the Metabox. " << endl;return;}uint64_t memoryBufferSize = 1024 * 1024;ofstream outfile(outputFileName, ofstream::binary);auto *memoryBuffer = new uint8_t[memoryBufferSize];const auto metaBoxFeatures = fileInfo.rootMetaBoxInformation.features;if (metaBoxFeatures & MetaBoxFeatureEnum::HasThumbnails){//reader->getMasterImages(itemIds);//const ImageId masterId = itemIds[0];cout << "The file have Thumbnail." << endl;// Thumbnail references ('thmb') are from the thumbnail image to the master image//reader->getReferencedToItemListByType(masterId, "thmb", itemIds);//const auto thumbnailId = itemIds[0];//if (reader->getItemDataWithDecoderParameters(thumbnailId.get(), memoryBuffer, memoryBufferSize) == ErrorCode::OK)//{// ...decode data and display the image, show master image later//    EncodePlay(memoryBuffer, memoryBufferSize);//   return;//}}// when the file is iPhone heic.Array<ImageId> gridIds;Grid gridData;if (reader->getItemListByType("grid", gridIds) == ErrorCode::OK &&(fileInfo.features & FileFeatureEnum::HasImageCollection) &&reader->getItem(gridIds[0].get(), gridData) == ErrorCode::OK){//return iphoneHeic(reader, gridData, outputFileName);qDebug("iphone heic");}// The image have collectionsif (fileInfo.features & FileFeatureEnum::HasImageCollection){Array<ImageId> itemIds;reader->getMasterImages(itemIds);// all the image itemsfor (unsigned int i = 0; i < itemIds.size; i++) {const ImageId masterId = itemIds[i];if (reader->getItemDataWithDecoderParameters(masterId.get(), memoryBuffer, memoryBufferSize) == ErrorCode::OK) {DecoderConfiguration decodeConf; // struct containingreader->getDecoderParameterSets(masterId.get(), decodeConf);auto decoSpeInfo = decodeConf.decoderSpecificInfo;for (unsigned int j = 0; j < decoSpeInfo.size; ++j) {auto entry = decoSpeInfo[j];outfile.write((const char *)entry.decSpecInfoData.begin(),entry.decSpecInfoData.size);}outfile.write(reinterpret_cast<const char*>(memoryBuffer), memoryBufferSize);if (i == 0){EncodePlay(memoryBuffer, memoryBufferSize);}}else { cout << "getItemDataWithDecoderParameters error" << endl; }};delete[] memoryBuffer;Reader::Destroy(reader);return;}// The image only have 1 master image.else if (fileInfo.features & FileFeatureEnum::HasSingleImage){Array<ImageId> itemIds;reader->getMasterImages(itemIds);// Find the item ID of the first master imageconst ImageId masterId = itemIds[0];if (reader->getItemDataWithDecoderParameters(masterId.get(), memoryBuffer, memoryBufferSize) == ErrorCode::OK){outfile.write(reinterpret_cast<const char*>(memoryBuffer), memoryBufferSize);cout << "Get the master image" << endl;// ffmpeg// Get HEVC decoder configurationEncodePlay(memoryBuffer, memoryBufferSize);}}cout << "There no image in the file MetaBox! ";Reader::Destroy(reader);return;
}

通过EncodePlay函数解析heic格式图像:

void heicExample::EncodePlay(uint8_t * pData, int nDataLength)
{AVPacket packet;av_init_packet(&packet);packet.data = (uint8_t*)pData;packet.size = nDataLength;packet.pts = (90000 / 25) * m_nFrameCounter++;int ret = -1;ret = avcodec_send_packet(m_pCodecContext, &packet);// Again EAGAIN is not expectedif (ret < 0){return;}while (!ret){ret = avcodec_receive_frame(m_pCodecContext, m_pSrcFrame);if (!ret){//成功解码一帧int w = m_pCodecContext->width;int h = m_pCodecContext->height;if (m_pRGBSwsContext == NULL){m_pRGBSwsContext = sws_getContext(w, h, m_pCodecContext->pix_fmt, w, h, AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);av_image_alloc(m_pFrameRGB->data, m_pFrameRGB->linesize, w, h, AV_PIX_FMT_RGB32, 1);}//转换图像格式,将解压出来的YUV420P的图像转换为RGB的图像sws_scale(m_pRGBSwsContext, (uint8_t const * const *)m_pSrcFrame->data, m_pSrcFrame->linesize, 0, h, m_pFrameRGB->data, m_pFrameRGB->linesize);//把这个RGB数据 用QImage加载QImage tmpImg((uchar *)m_pFrameRGB->data[0], m_pCodecContext->width, m_pCodecContext->height, QImage::Format_RGB32);image = tmpImg.copy();//把图像复制一份 传递给界面显示//emit signal_sendQImage(image);//ui.label->resize(w, h);//QImage img = image.scaled(ui.label->size(), Qt::KeepAspectRatio);//ui.label->setPixmap(QPixmap::fromImage(img));pixItem = new PixItem(QPixmap::fromImage(image));//将该图元对象添加到场景中,并设置此图元在场景中的位置为中心(0,0)m_graphicsScene->addItem(pixItem);pixItem->setPos(0, 0);//保存原来的颜色for (int x = 0; x < image.width(); x++) {for (int y = 0; y < image.height(); y++) {QColor *oldColor = new QColor(image.pixel(x, y));m_loldrgb.append(oldColor);}}av_frame_unref(m_pSrcFrame);}}
}

通过ffmpeg解码heic格式,然后转换成RGB32格式,最后通过画布进行展示。

heic(HEIF)格式图像处理(四)相关推荐

  1. heic(HEIF)格式图像处理(一)

    目录 开发思路 创建项目和界面设计 开发思路 上几节对开发环境的搭建,开发环境的配置进行的差不多了,从这节开始正式进入开发阶段. 简单介绍一下开发思路: 1,首先创建界面,由于开发环境是VS+Qt,界 ...

  2. 如何让Fresco支持HEIF/HEIC图片格式

    一.阅前准备 HEIF图片格式是什么? 高效率图像格式(High Efficiency Image Format ,HEIF)最早被苹果公司的 iPhone 所使用,并且也将用于 Google 的 A ...

  3. Android与HEIF格式图片适配方法

    本文字数:1490字 预计阅读时间:8分钟 一. 什么是HEIF图片 HEIF (High Efficiency Image File Format)是由动态图像专家组(MPEG)在2013年推出的新 ...

  4. 如何将png图片转为heif格式

    你可以使用图像处理软件或在线工具将PNG图片转换为HEIF格式.步骤如下: 打开图像处理软件,例如Adobe Photoshop. 选择要转换的PNG图片,然后点击"文件"菜单中的 ...

  5. 如何将heic图片格式转换jpg?

    如何将heic图片格式转换jpg?heic是苹果公司开发的一种图片格式,主要使用在苹果手机上面,虽然苹果手机用户挺多的,但是在windows电脑上还不能兼容heic格式图片,不能正常打开,而且很多网站 ...

  6. 怎么把heic图片格式转换jpg格式呢?

    Heic是许多图像格式中的一种.自从苹果将heic设置为图片存储的默认格式.与jpg格式相比,占用空间更少,图像质量更好.但是heic格式的图片在很多设备中无法直接打开,所以这时就要选择将heic转换 ...

  7. 将MSRA-TD500标签转换成逆时针输出标签+labeleme json格式转四个点的txt

    一.MSRA-TD500 : http://www.iapr-tc11.org/mediawiki/index.php/MSRA_Text_Detection_500_Database_%28MSRA ...

  8. AVI音视频封装格式学习(四)——linux系统C语言AVI格式音视频封装应用

    拖了很久的AVI音视频封装实例,花了一天时间终于调完了,兼容性不是太好,但作为参考学习使用应该没有问题.RIFF和AVI以及WAV格式,可以参考前面的一些文章.这里详细介绍将一个H264视频流和一个2 ...

  9. HEIF格式怎么转换图片?教你一个小技巧

    最近可能会有一些小伙伴发小,一些手机拍出的图片格式有些不对劲,点击查看是HEIF格式,这就让人一头雾水,尤其在导入windows系统的PS会出现报错,这是由于HEIF格式兼容性不足,导致对该格式不支持 ...

最新文章

  1. ISME:宿主性别可以决定肠道微生物对寄生虫感染的响应
  2. 构建商品评价的分类器
  3. python清空字典保留变量方法_python学习day06--02字典增删差改以及字符串的一些方法...
  4. 一种Android闪屏页实现方法(偏门别类)
  5. 语义分割损失函数系列(1):交叉熵损失函数
  6. 如何利用云原生技术构建现代化应用
  7. 用积木做了个无人机。
  8. 东北考生到南方学计算机,为啥东北考生都想去南方,而南方学生很少考东北,看看他们怎么说...
  9. Redis学习笔记——SpringDataRedis的使用
  10. C#中对注册表的操作指南
  11. java enum.isdefined_c# – Enum.IsDefined带有标记的枚举
  12. Python 奇葩语法
  13. java 接口和抽象类的区别_Java中的接口与抽象类:有什么区别?
  14. php显示上一次登陆的时间长,cookie实现显示上次登录时间的问题
  15. comps电磁场模拟软件_opera电磁仿真软件
  16. 整人电脑BAT小程序源码大全
  17. 【笔记】Protues仿真STM32的实现过程
  18. MATLAB(五) 图像处理--图像分割
  19. ie 无法打开服务器网页,无法打开internet站点怎么办
  20. 神经网络与深度学习期末考试满分过题库!

热门文章

  1. 对抗攻击与防御 (1):图像领域的对抗样本生成
  2. 【总结】对接达梦数据库DM8详细教程
  3. 【服务治理】服务熔断、服务降级、服务限流、流量削峰、错峰
  4. 使用WebRTC搭建前端视频聊天室系列文章
  5. 中望3D 2020中文版
  6. spring组件之gateway高级
  7. Internet of Things:物联网的应用领域涉及到方方面面,在工业、农业、环境、交通
  8. sqlmap蜜罐原理与实现
  9. 使用scrapy框架爬取汽车之家的图片(高清)
  10. 腾讯GAD:腾讯专家与Layabox官方团队联合解答H5游戏的制作与优化。