QiniuUtil.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package com.fdkankan.common.util;
  2. import com.qiniu.common.Zone;
  3. import com.qiniu.util.Auth;
  4. import com.qiniu.util.UrlSafeBase64;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.http.protocol.HTTP;
  12. import org.apache.http.util.EntityUtils;
  13. import java.io.IOException;
  14. import java.net.URLEncoder;
  15. public class QiniuUtil {
  16. public static String accessKey = "dlPPwgZky_F-iP8CbSbJpiAtAcqw3BYwb9rdHMrS";
  17. public static String secretKey = "YEtkLKDsImXB-8m1CT1zV_YwCwwGvrUvo2ktj9KZ";
  18. public static Zone zone = Zone.zone1();
  19. public static String bucket = "scene3d";
  20. public static String remoteUrl = "https://4dkanzhan.4dkankan.com/";
  21. public static void main(String[] args) throws IOException {
  22. // crop4K("4k.jpg", "http://www.baidu.com");
  23. // thumb("test111.jpg", "128", "128", "http://www.baidu.com");
  24. /*try{
  25. File f = new File("D:/usr/local/szggkfzlg/upload/20190125181036.png");
  26. FileInputStream fi = new FileInputStream(f);
  27. try{
  28. BufferedImage sourceImg =ImageIO.read(fi);//判断图片是否损坏
  29. int picWidth= sourceImg.getWidth(); //确保图片是正确的(正确的图片可以取得宽度)
  30. }catch (Exception e) {
  31. // TODO: handle exception
  32. fi.close();//关闭IO流才能操作图片
  33. }finally{
  34. fi.close();//最后一定要关闭IO流
  35. }
  36. }catch (Exception e ) {
  37. // TODO: handle exception
  38. System.out.println(e.toString());
  39. }*/
  40. getVideoFrame("a.mp4", "png", "6.010", null, null, null, "a_framw.png", "http://www.baidu.com");
  41. }
  42. /**
  43. * 视频帧缩略图接口(vframe)用于从视频流中截取指定时刻的单帧画面并按指定大小缩放成图片
  44. * @param key 文件名 a.mp4
  45. * @param suffixName 输出的目标截图格式,支持jpg、png等。
  46. * @param second 指定截取视频的时刻,单位:秒,精确到毫秒。
  47. * @param width 缩略图宽度,单位:像素(px),取值范围为1-3840, 可不传。
  48. * @param height 缩略图高度,单位:像素(px),取值范围为1-2160, 可不传。
  49. * @param rotate 指定顺时针旋转的度数,可取值为90、180、270、auto,默认为不旋转,可不传。
  50. * @param newKey 新文件名称
  51. * @param notifyURL 回调地址
  52. * @throws IOException
  53. */
  54. public static void getVideoFrame(String key, String suffixName, String second, Integer width, Integer height, Integer rotate, String newKey, String notifyURL) throws IOException {
  55. StringBuffer sb = new StringBuffer("vframe/").append(suffixName).append("/offset/").append(second);
  56. if (width != null){
  57. sb.append("/w/").append(width);
  58. }
  59. if (height != null){
  60. sb.append("/h/").append(height);
  61. }
  62. if (rotate != null){
  63. sb.append("/rotate/").append(rotate);
  64. }
  65. String fop = URLEncoder.encode(sb.toString(), "utf-8");
  66. StringBuffer ops = new StringBuffer("bucket=").append(bucket).append("&key=").append(key).append("&fops=").append(fop);
  67. String saveAs = UrlSafeBase64.encodeToString(bucket + ":" + newKey);
  68. ops.append("|saveas/").append(saveAs);
  69. ops.append("&notifyURL=").append(notifyURL);
  70. HttpPost post = new HttpPost("http://api.qiniu.com/pfop/");
  71. post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
  72. StringEntity entity = new StringEntity(ops.toString(), "utf-8");
  73. entity.setContentType("application/x-www-form-urlencoded");
  74. post.setEntity(entity);
  75. Auth auth = Auth.create(accessKey, secretKey);
  76. String si = auth.signRequest("http://api.qiniu.com/pfop/", ops.toString().getBytes(), "application/x-www-form-urlencoded");
  77. post.setHeader("Authorization", "QBox " + si);
  78. HttpClient client = new DefaultHttpClient();
  79. HttpResponse res = client.execute(post);
  80. String ret = EntityUtils.toString(res.getEntity(), "UTF-8");
  81. System.out.println(ret);
  82. }
  83. /**
  84. * 给视频添加水印
  85. * @param key a.mp4
  86. * @param text 4dage
  87. * @param fontColor #FFFF00
  88. * @param newKey new_a.mp4
  89. * @param notifyURL 回调地址
  90. * @throws IOException
  91. */
  92. public static void waterMark(String key, String text, String fontColor, String newKey, String notifyURL) throws IOException {
  93. String fop = "avthumb/mp4/";
  94. fop = URLEncoder.encode(fop, "utf-8");
  95. //水印文字
  96. String wmText = UrlSafeBase64.encodeToString(text);
  97. //水印文字的颜色
  98. String wmFontColor = UrlSafeBase64.encodeToString(fontColor);
  99. //设置avthumb 接口
  100. StringBuffer ops = new StringBuffer("bucket=").append(bucket).append("&key=").append(key).append("&fops=").append(fop);
  101. ops.append("avthumb/mp4/wmText/").append(wmText).append("/wmGravityText/NorthEast/wmFontColor/").append(wmFontColor);
  102. String saveAs = UrlSafeBase64.encodeToString(bucket + ":" + newKey);
  103. ops.append("|saveas/").append(saveAs);
  104. ops.append("&notifyURL=").append(notifyURL);
  105. HttpPost post = new HttpPost("http://api.qiniu.com/pfop/");
  106. post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
  107. StringEntity entity = new StringEntity(ops.toString(), "utf-8");
  108. entity.setContentType("application/x-www-form-urlencoded");
  109. post.setEntity(entity);
  110. Auth auth = Auth.create(accessKey, secretKey);
  111. String si = auth.signRequest("http://api.qiniu.com/pfop/", ops.toString().getBytes(), "application/x-www-form-urlencoded");
  112. post.setHeader("Authorization", "QBox " + si);
  113. HttpClient client = new DefaultHttpClient();
  114. HttpResponse res = client.execute(post);
  115. String ret = EntityUtils.toString(res.getEntity(), "UTF-8");
  116. System.out.println(ret);
  117. }
  118. /**
  119. * 将4K图片裁剪成64张512*512
  120. * @param key
  121. * @param notifyURL 回调地址 "http://wwww.cn/qn/notify&force=1"
  122. * @throws IOException
  123. */
  124. public static void crop4K(String key, String notifyURL) throws IOException {
  125. String fop = "imageMogr2/auto-orient/";
  126. fop = URLEncoder.encode(fop, "utf-8");
  127. String fileName = key.substring(0, key.lastIndexOf("."));
  128. String suffixName = key.substring(key.lastIndexOf("."));
  129. Auth auth = Auth.create(accessKey, secretKey);
  130. for (int i = 0; i < 8; i++) {
  131. for (int j = 0; j < 8; j++) {
  132. String ret = crop(key, notifyURL, fop, fileName, suffixName, auth, i, j);
  133. System.out.println(ret);
  134. }
  135. }
  136. }
  137. /**
  138. * 将2K图片裁剪成16张512*512
  139. * @param key
  140. * @param notifyURL 回调地址 "http://wwww.cn/qn/notify&force=1"
  141. * @throws IOException
  142. */
  143. public static void crop2k(String key, String notifyURL) throws IOException {
  144. String fop = "imageMogr2/auto-orient/";
  145. fop = URLEncoder.encode(fop, "utf-8");
  146. String fileName = key.substring(0, key.lastIndexOf("."));
  147. String suffixName = key.substring(key.lastIndexOf("."));
  148. Auth auth = Auth.create(accessKey, secretKey);
  149. for (int i = 0; i < 4; i++) {
  150. for (int j = 0; j < 4; j++) {
  151. String ret = crop(key, notifyURL, fop, fileName, suffixName, auth, i, j);
  152. System.out.println(ret);
  153. }
  154. }
  155. }
  156. /**
  157. * 将1K图片裁剪成4张512*512
  158. * @param key
  159. * @param notifyURL 回调地址 "http://wwww.cn/qn/notify&force=1"
  160. * @throws IOException
  161. */
  162. public static void crop1k(String key, String notifyURL) throws IOException {
  163. String fop = "imageMogr2/auto-orient/";
  164. fop = URLEncoder.encode(fop, "utf-8");
  165. String fileName = key.substring(0, key.lastIndexOf("."));
  166. String suffixName = key.substring(key.lastIndexOf("."));
  167. Auth auth = Auth.create(accessKey, secretKey);
  168. for (int i = 0; i < 2; i++) {
  169. for (int j = 0; j < 2; j++) {
  170. String ret = crop(key, notifyURL, fop, fileName, suffixName, auth, i, j);
  171. System.out.println(ret);
  172. }
  173. }
  174. }
  175. private static String crop(String key, String notifyURL, String fop, String fileName, String suffixName, Auth auth, int i, int j) throws IOException {
  176. StringBuffer ops = new StringBuffer("bucket=").append(bucket).append("&key=").append(key).append("&fops=").append(fop);
  177. ops.append("crop/!512x512a" + j * 512 + "a" + i * 512);
  178. String saveAs = UrlSafeBase64.encodeToString(bucket + ":" + fileName + "_" + j + "_" + i + suffixName);
  179. ops.append("|saveas/").append(saveAs);
  180. ops.append("&notifyURL=").append(notifyURL);
  181. HttpPost post = new HttpPost("http://api.qiniu.com/pfop/");
  182. post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
  183. StringEntity entity = new StringEntity(ops.toString(), "utf-8");
  184. entity.setContentType("application/x-www-form-urlencoded");
  185. post.setEntity(entity);
  186. String si = auth.signRequest("http://api.qiniu.com/pfop/", ops.toString().getBytes(), "application/x-www-form-urlencoded");
  187. post.setHeader("Authorization", "QBox " + si);
  188. HttpClient client = new DefaultHttpClient();
  189. HttpResponse res = client.execute(post);
  190. return EntityUtils.toString(res.getEntity(), "UTF-8");
  191. }
  192. /**
  193. * 将七牛云上的图片缩放成指定大小的图片
  194. * @param key
  195. * @param width
  196. * @param height
  197. * @param notifyURL
  198. * @throws IOException
  199. */
  200. public static void thumb(String key, String width, String height, String notifyURL, String newKey) throws IOException {
  201. String fop = "imageMogr2/auto-orient/";
  202. fop = URLEncoder.encode(fop, "utf-8");
  203. String fileName = key.substring(0, key.lastIndexOf("."));
  204. String suffixName = key.substring(key.lastIndexOf("."));
  205. Auth auth = Auth.create(accessKey, secretKey);
  206. StringBuffer ops = new StringBuffer("bucket=").append(bucket).append("&key=").append(key).append("&fops=").append(fop);
  207. ops.append("thumbnail/"+width+"x"+height+"!");
  208. //saveAs新生成文件的名称
  209. newKey = StringUtils.isNotEmpty(newKey) ? newKey : (fileName + "_thumb" + suffixName);
  210. String saveAs = UrlSafeBase64.encodeToString(bucket + ":" + newKey);
  211. ops.append("|saveas/").append(saveAs);
  212. ops.append("&notifyURL=").append(notifyURL);
  213. HttpPost post = new HttpPost("http://api.qiniu.com/pfop/");
  214. post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
  215. StringEntity entity = new StringEntity(ops.toString(), "utf-8");
  216. entity.setContentType("application/x-www-form-urlencoded");
  217. post.setEntity(entity);
  218. String si = auth.signRequest("http://api.qiniu.com/pfop/", ops.toString().getBytes(), "application/x-www-form-urlencoded");
  219. post.setHeader("Authorization", "QBox " + si);
  220. HttpClient client = new DefaultHttpClient();
  221. HttpResponse res = client.execute(post);
  222. System.out.println(EntityUtils.toString(res.getEntity(), "UTF-8"));
  223. }
  224. }