Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

90
LINES

< > BotCompany Repo | #1033821 // JavaCPP ffpmeg Webcam Capture Demo [OK on Linux x64]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 16081K of libraries. Click here for Pure Java version (131L/2K).

1  
!7
2  
3  
// from https://github.com/vzhn/ffmpeg-java-samples/blob/master/src/main/java/WebcamCapture.java
4  
5  
/*lib 1400538
6  
lib 1400539
7  
lib 1400540
8  
lib 1400541
9  
lib 1400542
10  
lib 1400543*/
11  
lib 1400544 // javacpp ffmpeg examples for linux x64
12  
13  
import org.bytedeco.javacpp.avcodec;
14  
15  
import static org.bytedeco.javacpp.avcodec.av_packet_unref;
16  
import static org.bytedeco.javacpp.avdevice.avdevice_register_all;
17  
import static org.bytedeco.javacpp.avformat.*;
18  
import static org.bytedeco.javacpp.avutil.*;
19  
20  
p { new WebcamCapture().start(); }
21  
22  
sclass WebcamCapture {
23  
  public static final int WIDTH = 800;
24  
  public static final int HEIGHT = 600;
25  
  private Canvas canvas;
26  
27  
  private void start() throws IOException {
28  
    JFrame frame = new JFrame();
29  
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
30  
    frame.setLayout(new BorderLayout());
31  
    canvas = new Canvas();
32  
    frame.add(canvas, BorderLayout.CENTER);
33  
    frame.setVisible(true);
34  
    frame.setSize(WIDTH, HEIGHT);
35  
    startCapture();
36  
  }
37  
38  
  private void startCapture() throws IOException {
39  
    av_log_set_level(AV_LOG_VERBOSE);
40  
    avdevice_register_all();
41  
    AVInputFormat v4l2 = av_find_input_format("v4l2");
42  
    if (v4l2 == null) {
43  
      throw new RuntimeException("v4l2 not found");
44  
    }
45  
    AVFormatContext v4l2Device = avformat_alloc_context();
46  
    if (v4l2Device == null) {
47  
      throw new RuntimeException("failed to alloc AVFormatContext");
48  
    }
49  
50  
    AVDictionary options = new AVDictionary();
51  
    av_dict_set(options, "input_format", "mjpeg", 0);
52  
53  
    if(avformat_open_input(v4l2Device, "/dev/video0", v4l2, options) != 0) {
54  
      throw new RuntimeException("Couldn't open input stream.\n");
55  
    }
56  
    av_dict_free(options);
57  
58  
    av_dump_format(v4l2Device, 0, "", 0);
59  
    if (v4l2Device.nb_streams() == 0) {
60  
      throw new RuntimeException("Stream not found!");
61  
    }
62  
63  
    avcodec.AVPacket pkt = new avcodec.AVPacket();
64  
    while (true) {
65  
      av_read_frame(v4l2Device, pkt);
66  
      byte[] data = new byte[pkt.size()];
67  
      pkt.data().get(data);
68  
      av_packet_unref(pkt);
69  
70  
      canvas.setImage(ImageIO.read(new ByteArrayInputStream(data)));
71  
    }
72  
  }
73  
}
74  
75  
sclass Canvas extends JPanel {
76  
  private BufferedImage bufferedImage;
77  
78  
  @Override
79  
  public void paint(Graphics g) {
80  
    Graphics2D g2d = (Graphics2D) g;
81  
    if (bufferedImage != null) {
82  
      g2d.drawImage(bufferedImage, 0, 0, null);
83  
    }
84  
  }
85  
86  
  public void setImage(BufferedImage bufferedImage) {
87  
    this.bufferedImage = bufferedImage;
88  
    repaint();
89  
  }
90  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033821
Snippet name: JavaCPP ffpmeg Webcam Capture Demo [OK on Linux x64]
Eternal ID of this version: #1033821/6
Text MD5: 849e8d50570ac8d97c81579222e67bc4
Transpilation MD5: d7cb066752c09640a8dd3b7dd252cceb
Author: stefan
Category: javax / gazelle v
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-06 18:50:47
Source code size: 2579 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 102 / 584
Version history: 5 change(s)
Referenced in: [show references]