当前位置:   article > 正文

iPhone拍摄的照片(heic格式)转jpg格式---Java_imageio-heic

imageio-heic

1.只能将heic格式文件读取成二进制流才能成功---用imageIO包读取返回结果为null

  1. /**
  2. * 将二进制转换成图片保存---------场景(将heic格式转化为jpg格式)
  3. * @param imgStr 二进制流转换的字符串
  4. * @param imgPath 图片的保存路径
  5. * @param imgName 图片的名称
  6. * @return true:保存正常 false:保存失败
  7. */
  8. // @ClearInterceptor(ClearLayer.ALL)
  9. public boolean saveToImgByBytes(File imgFile, String imgPath, String imgName){
  10. boolean stateInt = true;
  11. if(imgFile.length() > 0){
  12. try {
  13. File file=new File(imgPath, imgName);//可以是任何图片格式.jpg,.png等
  14. FileOutputStream fos=new FileOutputStream(file);
  15. FileInputStream fis = new FileInputStream(imgFile);
  16. byte[] b = new byte[1024];
  17. int nRead = 0;
  18. while ((nRead = fis.read(b)) != -1) {
  19. fos.write(b, 0, nRead);
  20. }
  21. fos.flush();
  22. fos.close();
  23. fis.close();
  24. } catch (Exception e) {
  25. stateInt = false;
  26. e.printStackTrace();
  27. } finally {
  28. }
  29. }
  30. return stateInt;
  31. }

 

 

2.下载的时候直接判断是否是heic格式,如果是,直接转码,写成jpg格式保存

  1. // 参数filename----写上后缀就能直接转码成jpg格式
  2. /**
  3. * @从制定URL下载文件并保存到指定目录
  4. * @param filePath 文件将要保存的目录
  5. * @param method 请求方法,包括POST和GET
  6. * @param url 请求的路径
  7. * @return
  8. */
  9. public String downloadFromUrl(String url, String filePath, String method, String fileName){
  10. // fileName = 全景图的name-枚举 + 序号
  11. //创建不同的文件夹目录
  12. File file = new File(filePath);
  13. //判断文件夹是否存在
  14. if (!file.exists()) {
  15. //如果文件夹不存在,则创建新的的文件夹
  16. file.mkdirs();
  17. }
  18. FileOutputStream fileOut = null;
  19. HttpURLConnection conn = null;
  20. InputStream inputStream = null;
  21. String contentLength = null;
  22. try {
  23. // 建立链接
  24. URL httpUrl=new URL(url);
  25. conn = (HttpURLConnection) httpUrl.openConnection();
  26. //以Post方式提交表单,默认get方式
  27. conn.setRequestMethod(method);
  28. conn.setDoInput(true);
  29. conn.setDoOutput(true);
  30. // post方式不能使用缓存
  31. conn.setUseCaches(false);
  32. //连接指定的资源
  33. conn.connect();
  34. //获取网络输入流
  35. inputStream=conn.getInputStream();
  36. BufferedInputStream bis = new BufferedInputStream(inputStream);
  37. //判断文件的保存路径后面是否以/结尾
  38. if (!filePath.endsWith("/")) {
  39. filePath += "/";
  40. }
  41. //写入到文件(注意文件保存路径的后面一定要加上文件的名称)
  42. fileOut = new FileOutputStream(filePath + fileName);
  43. BufferedOutputStream bos = new BufferedOutputStream(fileOut);
  44. byte[] buf = new byte[4096];
  45. int length = bis.read(buf);
  46. //保存文件
  47. while(length != -1) {
  48. bos.write(buf, 0, length);
  49. length = bis.read(buf);
  50. }
  51. // 获取response header值
  52. contentLength = getContentLength(conn);
  53. bos.close();
  54. bis.close();
  55. conn.disconnect();
  56. } catch (Exception e) {
  57. LOGGER.error("下载文件异常:{}", e);
  58. }
  59. return contentLength;
  60. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/931072
推荐阅读
相关标签
  

闽ICP备14008679号