ffmepg入门学习九 java实现图片截图

 1.准备

需求,形成1天每个小时的画像,并合成一个图片。

思路:每个小时抽取一张图片,再把这种图片截取1/24并存放,后面,再把这24张图片合成一张图片。

准备一张原图

截图前:

 截图后:(下午14点的截图)

 

 


   
   
   
   
  1. package com.qihui.qxj.utils;
  2. import java.awt.Rectangle;
  3. import java.awt.image.BufferedImage;
  4. import java.io.*;
  5. import java.util.Iterator;
  6. import javax.imageio.ImageIO;
  7. import javax.imageio.ImageReadParam;
  8. import javax.imageio.ImageReader;
  9. import javax.imageio.stream.ImageInputStream;
  10. public class ImageUtils {
  11. public static void cutJPG(InputStream input, OutputStream out, int x,
  12. int y, int width, int height) throws IOException {
  13. ImageInputStream imageStream = null;
  14. try {
  15. Iterator readers = ImageIO.getImageReadersByFormatName( "jpg");
  16. ImageReader reader = readers.next();
  17. imageStream = ImageIO.createImageInputStream(input);
  18. reader.setInput(imageStream, true);
  19. ImageReadParam param = reader.getDefaultReadParam();
  20. System.out.println(reader.getWidth( 0));
  21. System.out.println(reader.getHeight( 0));
  22. Rectangle rect = new Rectangle(x, y, width, height);
  23. param.setSourceRegion(rect);
  24. BufferedImage bi = reader.read( 0, param);
  25. ImageIO.write(bi, "jpg", out);
  26. } finally {
  27. imageStream.close();
  28. }
  29. }
  30. public static void cutPNG(InputStream input, OutputStream out, int x,
  31. int y, int width, int height) throws IOException {
  32. ImageInputStream imageStream = null;
  33. try {
  34. Iterator readers = ImageIO.getImageReadersByFormatName( "png");
  35. ImageReader reader = readers.next();
  36. imageStream = ImageIO.createImageInputStream(input);
  37. reader.setInput(imageStream, true);
  38. ImageReadParam param = reader.getDefaultReadParam();
  39. System.out.println(reader.getWidth( 0));
  40. System.out.println(reader.getHeight( 0));
  41. Rectangle rect = new Rectangle(x, y, width, height);
  42. param.setSourceRegion(rect);
  43. BufferedImage bi = reader.read( 0, param);
  44. ImageIO.write(bi, "png", out);
  45. } finally {
  46. imageStream.close();
  47. }
  48. }
  49. public static void cutImage(InputStream input, OutputStream out, String type,int x,
  50. int y, int width, int height) throws IOException {
  51. ImageInputStream imageStream = null;
  52. try {
  53. String imageType=( null==type|| "".equals(type))? "jpg":type;
  54. Iterator readers = ImageIO.getImageReadersByFormatName(imageType);
  55. ImageReader reader = readers.next();
  56. imageStream = ImageIO.createImageInputStream(input);
  57. reader.setInput(imageStream, true);
  58. ImageReadParam param = reader.getDefaultReadParam();
  59. Rectangle rect = new Rectangle(x, y, width, height);
  60. param.setSourceRegion(rect);
  61. BufferedImage bi = reader.read( 0, param);
  62. ImageIO.write(bi, imageType, out);
  63. } finally {
  64. imageStream.close();
  65. }
  66. }
  67. public static void main(String[] args) throws Exception {
  68. String imgName = "D:\\test.jpg";
  69. BufferedImage sourceImg = ImageIO.read( new FileInputStream(imgName));
  70. //获取原始图片的宽高
  71. int height = sourceImg.getHeight();
  72. int width = sourceImg.getWidth();
  73. sourceImg.flush();
  74. int x = (width / 24) * ( 14);
  75. int y = 0;
  76. ImageUtils.cutJPG( new FileInputStream(imgName),
  77. new FileOutputStream( "D:\\test123.jpg"), x,y,width,height);
  78. /* ImageUtils.cutPNG(new FileInputStream("c:\\1.png"),
  79. new FileOutputStream("c:\\test3.png"), 0,0,50,40); */
  80. }
  81. }

本文章暂时只实现图片的截取,把一张图片切割成24份,取对应小时对应在原图的宽高。如何把24张图片合成一张图片。且看下下文。

如果你热衷技术,喜欢交流,欢迎加入我们! 

欢迎关注公众号“程序猿学社”

       1.免费领取各种学习视频,学习相关java各个技术点面试知识

       2.有相关学习疑问或者面试困惑,也可以通过公众号私信博主

      3.通过公众号进入java交流群,不闲聊,只谈技术

ffmepg入门学习九 java实现图片截图_第1张图片

发布了277 篇原创文章 · 获赞 514 · 访问量 21万+

转载自:https://blog.csdn.net/qq_16855077/article/details/90378173

你可能感兴趣的:(FFmpeg音视频学习)