score:1

Accepted answer

there are couple of issues i faced while reproducing your issue, so let me guide you through them.

  1. first of all, you use the old version of the framework. the current one is 3.2, so consider upgrading. i used the newest version.

  2. you should not comment out the camera.isopened() check since if the camera is not opened there is no sense to proceed. i assume, that your app couldn't pass that check, so you decided to skip that :) but in order to open the camera properly you should either do this or this. the first link worked for me (the only change is that for 3.2 version the path is \opencv\build\x64\vc14\bin and i had to reboot computer)

the code is pretty much the same as you have so far with only difference for the latest version:

import org.opencv.core.core;
import org.opencv.core.mat;
import org.opencv.imgcodecs.imgcodecs;
import org.opencv.videoio.videocapture;

import javax.swing.*;
import java.awt.image.bufferedimage;
import java.awt.image.databufferbyte;
import java.awt.image.writableraster;
import java.nio.file.paths;

public class vasanth {

    public static void main(string[] args) {
        system.loadlibrary(core.native_library_name);
        string filepath = "g:\\pictures\\roxtar vassy\\early days in apsce.wmv";
        if (!paths.get(filepath).tofile().exists()){
             system.out.println("file " + filepath + " does not exist!");
             return;
        }

        videocapture camera = new videocapture(filepath);

        if (!camera.isopened()) {
            system.out.println("error! camera can't be opened!");
            return;
        }
        mat frame = new mat();

        while(true){
            if (camera.read(frame)){
                system.out.println("frame obtained");
                system.out.println("captured frame width " +
                        frame.width() + " height " + frame.height());
                imgcodecs.imwrite("camera.jpg", frame);
                system.out.println("ok");
                break;
            }
        }

        bufferedimage bufferedimage = mattobufferedimage(frame);
        showwindow(bufferedimage);
        camera.release();
    }

    private static bufferedimage mattobufferedimage(mat frame) {
        int type = 0;
        if (frame.channels() == 1) {
            type = bufferedimage.type_byte_gray;
        } else if (frame.channels() == 3) {
            type = bufferedimage.type_3byte_bgr;
        }
        bufferedimage image = new bufferedimage(frame.width(), frame.height(), type);
        writableraster raster = image.getraster();
        databufferbyte databuffer = (databufferbyte) raster.getdatabuffer();
        byte[] data = databuffer.getdata();
        frame.get(0, 0, data);

        return image;
    }

    private static void showwindow(bufferedimage img) {
        jframe frame = new jframe();
        frame.getcontentpane().add(new jlabel(new imageicon(img)));
        frame.setdefaultcloseoperation(windowconstants.exit_on_close);
        frame.setsize(img.getwidth(), img.getheight() + 30);
        frame.settitle("image captured");
        frame.setvisible(true);
    }
}

note, that in order to demonstrate that this piece of code works i use simple swing jframe:

enter image description here

score:0

i was facing the same issue which is camera.isopened() returning false. this is how i resolved it.

string filepath = "c:\\aslam_face_detection.mp4";
videocapture camera = new videocapture(filepath);
camera.open(filepath); // this is the line i added
camera.isopened(); // returns true. worked.

Related Query

More Query from same tag