HEIC格式转换为jpg(C#)

自从iOS11以后,苹果手机默认的照片格式为HEIC,该格式上传至服务器后,不能被其他应用使用,包括windows目前都无法打开。所以在服务端需要有自动处理的工具来将HEIC处理为JPG格式,下面是一个小示例,大家可以根据示例来部署FileHandler,实现文件上传后的自动转换。

主要是借用这个团队的劳动成果:https://liuziangexit.com/

代码可以在该链接下载:http://download.csdn.net/download/wuwo333/10112121

需要在bin文件夹加入下面三个lib:

HUD.dll

opencv_ffmpeg330_64.dll

opencv_world330.dll

 static void Main(string[] args)
        {
            var heif_data = invoke_dll.read_heif("D:/1.heic");
            invoke_dll.invoke_heif_to_jpg(heif_data, 80, "D:/2.jpgtmp").Save("D:/3.jpg", ImageFormat.Jpeg);
            Console.ReadLine();
        }

invoke_dll类

  class invoke_dll
    {

        [DllImport("HUD.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        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);

        public static byte[] read_heif(string filename)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(filename, FileMode.Open);
                System.IO.BinaryReader br = new BinaryReader(fs);
                byte[] byte_array = br.ReadBytes((int)fs.Length);
                fs.Close();
                return byte_array;
            }
            catch (Exception ex)
            {
                fs.Close();
                throw ex;
            }
        }

        public static unsafe Image invoke_heif_to_jpg(byte[] heif_bin, int jpg_quality, string temp_filename)
        {
            var output_buffer = new byte[heif_bin.Length * 10];
            byte[] temp_filename_byte_array = System.Text.Encoding.Default.GetBytes(temp_filename);
            fixed (byte* input = &heif_bin[0], output = &output_buffer[0], temp_filename_byte = &temp_filename_byte_array[0])
            {
                heif2jpg(input, heif_bin.Length, jpg_quality, output, heif_bin.Length * 10, temp_filename_byte);
            }
            return Image.FromStream(new MemoryStream(output_buffer));
        }
    }



  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
在Go语言中,你可以使用第三方库来实现HEIC格式图片批量转换为JPG格式。一个常用的库是github.com/dsoprea/go-heic-convert。以下是一个简单的示例代码: ```go package main import ( "fmt" "io/ioutil" "os" "path/filepath" "github.com/dsoprea/go-heic-convert/convert" ) func main() { // 输入HEIC图片所在文件夹路径 inputDir := "/path/to/input/directory" // 输出JPG图片所在文件夹路径 outputDir := "/path/to/output/directory" // 获取输入文件夹下的所有HEIC文件 files, err := ioutil.ReadDir(inputDir) if err != nil { fmt.Printf("无法读取输入文件夹:%v\n", err) os.Exit(1) } // 遍历每个HEIC文件并进行转换 for _, file := range files { if !file.IsDir() && filepath.Ext(file.Name()) == ".heic" { inputFile := filepath.Join(inputDir, file.Name()) outputFile := filepath.Join(outputDir, file.Name()+".jpg") err = convert.ConvertHEICFile(inputFile, outputFile) if err != nil { fmt.Printf("转换失败:%s\n", inputFile) } else { fmt.Printf("转换成功:%s\n", inputFile) } } } } ``` 请确保在运行代码之前,你已经安装了`github.com/dsoprea/go-heic-convert`库。可以使用以下命令进行安装: ```shell go get -u github.com/dsoprea/go-heic-convert/... ``` 注意:该库依赖于ImageMagick和libheif库。在使用之前,请确保这两个库已经正确安装并配置好环境变量。 运行上述代码时,将输入文件夹路径和输出文件夹路径替换为实际的路径。代码将遍历输入文件夹中的所有HEIC文件,并将其转换为JPG格式并保存在输出文件夹中。转换成功的文件将打印在控制台上,而转换失败的文件将显示相应的错误消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值