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).

!7

// from https://github.com/vzhn/ffmpeg-java-samples/blob/master/src/main/java/WebcamCapture.java

/*lib 1400538
lib 1400539
lib 1400540
lib 1400541
lib 1400542
lib 1400543*/
lib 1400544 // javacpp ffmpeg examples for linux x64

import org.bytedeco.javacpp.avcodec;

import static org.bytedeco.javacpp.avcodec.av_packet_unref;
import static org.bytedeco.javacpp.avdevice.avdevice_register_all;
import static org.bytedeco.javacpp.avformat.*;
import static org.bytedeco.javacpp.avutil.*;

p { new WebcamCapture().start(); }

sclass WebcamCapture {
  public static final int WIDTH = 800;
  public static final int HEIGHT = 600;
  private Canvas canvas;

  private void start() throws IOException {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    canvas = new Canvas();
    frame.add(canvas, BorderLayout.CENTER);
    frame.setVisible(true);
    frame.setSize(WIDTH, HEIGHT);
    startCapture();
  }

  private void startCapture() throws IOException {
    av_log_set_level(AV_LOG_VERBOSE);
    avdevice_register_all();
    AVInputFormat v4l2 = av_find_input_format("v4l2");
    if (v4l2 == null) {
      throw new RuntimeException("v4l2 not found");
    }
    AVFormatContext v4l2Device = avformat_alloc_context();
    if (v4l2Device == null) {
      throw new RuntimeException("failed to alloc AVFormatContext");
    }

    AVDictionary options = new AVDictionary();
    av_dict_set(options, "input_format", "mjpeg", 0);

    if(avformat_open_input(v4l2Device, "/dev/video0", v4l2, options) != 0) {
      throw new RuntimeException("Couldn't open input stream.\n");
    }
    av_dict_free(options);

    av_dump_format(v4l2Device, 0, "", 0);
    if (v4l2Device.nb_streams() == 0) {
      throw new RuntimeException("Stream not found!");
    }

    avcodec.AVPacket pkt = new avcodec.AVPacket();
    while (true) {
      av_read_frame(v4l2Device, pkt);
      byte[] data = new byte[pkt.size()];
      pkt.data().get(data);
      av_packet_unref(pkt);

      canvas.setImage(ImageIO.read(new ByteArrayInputStream(data)));
    }
  }
}

sclass Canvas extends JPanel {
  private BufferedImage bufferedImage;

  @Override
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    if (bufferedImage != null) {
      g2d.drawImage(bufferedImage, 0, 0, null);
    }
  }

  public void setImage(BufferedImage bufferedImage) {
    this.bufferedImage = bufferedImage;
    repaint();
  }
}

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: 218 / 889
Version history: 5 change(s)
Referenced in: [show references]