Iphone的HEIC圖片文件格式轉換及相關開發工具


1、Windows 10安裝插件查看heic圖片

Windows10上默認無法打開Apple的HEVC&HEIC格式文件,需要安裝相應的擴展,在 Microsoft Store里查找會發現“HEVC視頻擴展”是一個付費應用!(在國外的Microsoft Store里此擴展是免費的。實際上在品牌機預裝Win10中會自帶一個“來自設備制造商的HEVC視頻擴展”,功能和付費的是一樣的。但是你無法在Microsoft Store內直接搜索到這個擴展。

解決辦法:
瀏覽器中直接打開如下鏈接:
https://www.microsoft.com/en-us/p/hevc-video-extensions-from-device-manufacturer/9n4wgh0z6vhq
點擊“獲取”就會自動打開Microsoft Store來安裝此擴展了。

2、批量轉換工具
如果需要批量的轉換工具,可以下載下面的免費工具
 
里面附帶的dll可以供開發編程使用
opencv_ffmpeg330_64.dll
opencv_world330.dll
HUD.dll
 
3、程序處理
可以下載下面的Dll,這個dll封裝了opencv相關api接口,提供了轉換的接口,接口可以通過C、C#、JNI等來調用
 
C語言調用工具
 1 #include <iostream>
 2 #include <cstring>
 3 #include <fstream>
 4 #include <sstream>
 5 #include <windows.h>
 6 
 7 using namespace std;
 8 typedef  void (__stdcall *heif2jpg)(const char heif_bin[], int input_buffer_size, const int jpg_quality,
 9                                     char output_buffer[], int output_buffer_size, const char* input_temp_filename,
10                                     int* copysize, bool include_exif, bool color_profile, const char icc_bin[], int icc_size);
11 //讀取文件內容
12 ostringstream readBinFile(char* filename,int &size)
13 {
14     fstream fs;
15     fs.open(filename, ios::in|ios::binary);
16     ostringstream oss;
17     oss << fs.rdbuf();
18     size=oss.str().length();
19     fs.close();
20     return oss;
21 }
22 
23 int main(int argc, char *argv[])
24 {
25     cout <<"Heic Convert Tool V1.0 <By midea0978>"<<endl;
26     cout <<"====================================\n"<<endl;
27     int quality=80;
28     if(argc<2)
29     {
30         cout <<"heic.exe <heic文件名> [jpg質量1-100,默認80]\n\n"<<endl;
31         return -1;
32     }
33     if(argc>=3) quality=atoi(argv[2]);
34     HMODULE hModule;
35     hModule = LoadLibraryA("HUD.dll");
36     if ( NULL == hModule)
37     {
38         DWORD error_id=GetLastError();
39         cout<<"加載hud.dll失敗:"<<error_id<<endl;
40         return -1;
41     }
42     heif2jpg func = NULL;
43     func = (heif2jpg)GetProcAddress(hModule, "heif2jpg");
44     char drive[_MAX_DRIVE];
45     char dir[_MAX_DIR];
46     char fname[_MAX_FNAME];
47     char ext[_MAX_EXT];
48     _splitpath(argv[1], drive, dir, fname, ext);
49     string ofilename;
50     ofilename+=drive;
51     ofilename+=dir;
52     ofilename+=fname;
53     ofilename+=".jpg";
54     printf("輸出JPG文件 %s\n",ofilename.c_str());
55     int imgsize=0;
56     ostringstream heicdata=readBinFile(argv[1],imgsize);
57     int outsize=10*imgsize;
58     char* outbuffer;
59     int copysize[]= {0};
60     outbuffer = (char*)malloc(outsize);
61     memset(outbuffer, 0, outsize);
62     char temppath[MAX_PATH];
63     GetTempPath(MAX_PATH,temppath);
64     string tempfile;
65     tempfile+=temppath;
66     tempfile+="\\temp.dat";
67     func(heicdata.str().c_str(),imgsize,quality,outbuffer,outsize,tempfile.c_str(),copysize,true,false,NULL,0);
68     ofstream outfile(ofilename,ios::binary);
69     outfile.write(outbuffer,copysize[0]);
70     outfile.close();
71     remove(tempfile.c_str());
72     FreeLibrary(hModule);
73     return 0;
74 }

 

對應C#的調用方式,代碼來自於下面的URL

 1 [DllImport("HUD.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
 2 private unsafe extern static void heif2jpg(byte* heif_bin, int input_buffer_size, int jpg_quality, byte* ouput_buffer, int output_buffer_size, byte* temp_filename, int* copysize, bool include_exif, bool color_profile, byte* icc_bin, int icc_size);
 3 
 4 [DllImport("HUD.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
 5 private unsafe extern static void getexif(byte* heif_bin, int input_buffer_size, byte* ouput_buffer, int output_buffer_size, int* copysize);
 6 
 7 public static unsafe byte[] invoke_heif2jpg(byte[] heif_bin, int jpg_quality, string temp_filename, ref int copysize, bool include_exif, bool color_profile)
 8 {
 9     if (color_profile == true && DisplayP3Profile.Length == 1)
10         throw new Exception();//沒有ICC卻指定要寫入ICC
11 
12     var output_buffer = new byte[heif_bin.Length * 10];
13     byte[] temp_filename_byte_array = System.Text.Encoding.Default.GetBytes(temp_filename);
14     int[] copysize_array = new int[1] { 0 };
15     fixed (byte* input = &heif_bin[0], output = &output_buffer[0], temp_filename_byte = &temp_filename_byte_array[0], icc_ptr = &DisplayP3Profile[0])
16     fixed (int* copysize_p = &copysize_array[0])
17     {
18         heif2jpg(input, heif_bin.Length, jpg_quality, output, output_buffer.Length, temp_filename_byte, copysize_p, include_exif, color_profile, icc_ptr, DisplayP3Profile.Length);
19     }
20     copysize = copysize_array[0];
21     return output_buffer;
22 }
23 
24 public static unsafe string invoke_getexif(byte[] heif_bin, ref int copysize)
25 {
26     var output_buffer = new byte[65535];
27     int[] copysize_array = new int[1] { 0 };
28     fixed (byte* input = &heif_bin[0], output = &output_buffer[0])
29     fixed (int* copysize_p = &copysize_array[0])
30     {
31         getexif(input, heif_bin.Length, output, output_buffer.Length, copysize_p);
32     }
33     copysize = copysize_array[0];
34     return Encoding.Default.GetString(output_buffer, 0, copysize);
35 }

 

  

詳細代碼參考:https://github.com/liuziangexit/HEIF-Utility/blob/master/HEIF%20Utility/invoke_dll.cs  

 

其他的開源庫:

https://github.com/libvips/libvips

ibvips是一個圖像處理庫。與類似的庫相比,libvips運行迅速且幾乎不占用內存。libvips是根據LGPL 2.1+許可的。

它具有約300種運算, 涵蓋算術,直方圖,卷積,形態運算,頻率濾波,顏色,重采樣,統計等。它支持多種數字類型,從8位int到128位復數。圖像可以具有任意數量的波段。它支持各種圖像格式,包括JPEG,TIFF,PNG,WebP,HEIC,FITS,Matlab,OpenEXR,PDF,SVG,HDR,PPM / PGM / PFM,CSV,GIF,分析,NIfTI,DeepZoom和OpenSlide 。它還可以通過ImageMagick或GraphicsMagick加載圖像,使其與DICOM等格式一起使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM