ffmepg入门学习三 java通过摄像头截取图片

1.准备

      有可连接的摄像头,例如大华、海康

   ffmpeg下载:https://blog.csdn.net/qq_16855077/article/details/89839708

   例如下面rtsp各个品牌的url不同,这里就不过多的说明

   rtsp://admin:[email protected]:554/Streaming/Channels/101?transportmode=unicast

 2.截图


  
  
  1. package com.qihui.qxj.services.system;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import org.junit.Test;
  7. public class Test1 {
  8. @ Test
  9. public void test1( ) {
  10. List commend = new ArrayList();
  11. commend. add( "c:/bin/ffmpeg.exe");
  12. commend. add( "-i");
  13. commend. add( "rtsp://admin:[email protected]:554/Streaming/Channels/101?transportmode=unicast");
  14. commend. add( "-s");
  15. commend. add( "4096*1800");
  16. commend. add( "-b");
  17. commend. add( "4M");
  18. commend. add( "-y");
  19. commend. add( "-f");
  20. commend. add( "image2");
  21. commend. add( "-an");
  22. commend. add( "-loglevel");
  23. commend. add( "8");
  24. commend. add( "d:/project/direct/40/a1.jpg");
  25. Process p = null;
  26. try {
  27. ProcessBuilder builder = new ProcessBuilder(commend);
  28. builder.command(commend);
  29. p = builder.start();
  30. p.getOutputStream().close();
  31. doWaitFor(p);
  32. p.destroy();
  33. System. out.println( 1122);
  34. } catch (Exception e) {
  35. PrintCatchErrorMsg.Print(e, "Part", "getRSTPPicture.catch", "Exception");
  36. p.destroy();
  37. }
  38. }
  39. public static int doWaitFor(Process p) {
  40. InputStream in = null;
  41. InputStream err = null;
  42. int exitValue = -1;
  43. try {
  44. in = p.getInputStream();
  45. err = p.getErrorStream();
  46. boolean finished = false;
  47. while(!finished) {
  48. try {
  49. Character c;
  50. while( in.available() > 0) {
  51. c = new Character(( char) in.read());
  52. System. out.print(c);
  53. }
  54. while(err.available() > 0) {
  55. c = new Character(( char)err.read());
  56. System. out.print(c);
  57. }
  58. exitValue = p.exitValue();
  59. finished = true;
  60. } catch (IllegalThreadStateException var19) {
  61. Thread.currentThread();
  62. Thread.sleep( 500L);
  63. }
  64. }
  65. } catch (Exception var20) {
  66. } finally {
  67. try {
  68. if ( in != null) {
  69. in.close();
  70. }
  71. } catch (IOException var18) {
  72. }
  73. if (err != null) {
  74. try {
  75. err.close();
  76. } catch (IOException var17) {
  77. }
  78. }
  79. }
  80. return exitValue;
  81. }
  82. }

 c:/bin/ffmpeg.exe  ffmepeg的位置

 -i  地址

-s  分辨率

-b 码率

-y 如果文件存在,再替换

注意:如果内网环境可以截图,外网环境截图失败,需要把协议转为tcp,增加两行代码


  
  
  1. commend. add( "-rtsp_transport");
  2. commend. add( "tcp");

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

发布了42 篇原创文章 · 获赞 115 · 访问量 1万+

1.准备

猜你喜欢

转载自blog.csdn.net/luoyong_blog/article/details/104497416