I have a program that uses OpenCV to make some pictures with the webcam.
When I run the application in my IDE(NetBeans) it works like a charm, but when I try to run the jar file it doesn't even show what the webcam sees in our JFrame. Does anyone know how to solve this?
public void run(){
try {
grabber = new VideoInputFrameGrabber(0);
grabber.start();
while (active) {
IplImage originalImage = grabber.grab();
Label.setIcon(new ImageIcon( originalImage.getBufferedImage() ));
}
grabber.stop();
grabber.flush();
} catch (Exception ex) {
//Logger.getLogger(ChPanel.class.getName()).log(Leve l.SEVERE, null, ex);
}
}
public BufferedImage saveImage(){
IplImage img;
try {
//capture image
img = grabber.grab();
// save to file
File outputfile = new File(Project.getInstance().getFileURLStr() + " capture" + fotoCount++ + ".jpg");
ImageIO.write(img.getBufferedImage(), "jpg", outputfile);
//get file and set it in the project library
BufferedImage ImportFile = ImageIO.read(outputfile);
Project p = Project.getInstance();
MainScreen ms = MainScreen.getInstance();
ImageIcon takenPhoto = new ImageIcon(ImportFile);
p.setNextImage(takenPhoto);
ms.setPanels();
return ImportFile;
} catch (com.googlecode.javacv.FrameGrabber.Exception e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
When I run it out of CMD, it does show the image on my JFrame. But when I try to take the picture it shows this message: "Failed to write core dump. Minidumps are not enabled by default on client version of windows"
I already found the problem, I had about 3 versions of Java installed, wich propably wasn't okay :p
Related
try {
FileInputStream f = new FileInputStream("proj.bin");
ObjectInputStream o = new ObjectInputStream(f);
this.FigureList = (ArrayList<Figure>) o.readObject();
o.close();
} catch (Exception x) {
}
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
FileOutputStream f = new FileOutputStream("proj.bin");
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(FigureList);
o.flush();
o.close();
} catch (Exception x) {
}
System.exit(0);
}
});
I've been using this code to save the infos of my panel(Figure position, color and those kind of things), so whenever I run the code again, there will be things already there. Can save it as SVG and use this svg file in others apps? I've tried to change ".bin" for ".svg" but it seems it's not that easy.
You can see my whole code here
i am trying to load an image using this methods.
I am asking for my image here:
private BufferedImage image = controller.loadImage("/paddle.png");
And the loadImage method is
public BufferedImage loadImage(String path) {
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getResource(path));
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
I added "res" to my class folder also but nothing seems to work.
My project explorer looks like this : http://prntscr.com/hmar35
EDIT: I also tried URL loading but failed to work either,the code for URL loading is:
public BufferedImage loadImage(String path) {
BufferedImage image = null;
try {
URL link = Controller.class.getResource(path);
image = (BufferedImage) Toolkit.getDefaultToolkit().getImage(link);
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
Try using
ImageIO.read(getClass().getClassLoader().getResourceAsStream(path));
where path = "paddle.png" in your case.
If your res folder is set as a source folder, and "paddle.png" is located directly inside of the res folder, you do not need to add a "/" to the file path.
Note: Also make sure you refresh your project to make sure all files are recognized.
Below is the code that I am trying to use.
InputStream in = new BufferedInputStream(Tester.class.getClassLoader().getResourceAsStream("appResources/img/GESS.png"));
Image image=null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(image);
'image' comes null when I am printing it.
Try this:
InputStream in = Tester.class.getResourceAsStream("your/path");
Image image=null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(image);
The value or your/path is either appResources/img/GESS.png or /appResources/img/GESS.png depending on your maven configuration and the directory you setup for your project.
For instance, if you add the following entry to your pom.xml file:
<resources>
<resource>
<directory>src/main/appResources</directory>
</resource>
</resources>
Then, you can get the same resource by using a shorter path since your program knows where to look for resources:
InputStream in = Tester.class.getResourceAsStream("img/GESS.png");
Image image=null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(image);
More info here and here.
you can use InputStream instream=Thread.currentThread().getContextClassLoader().getResourceAsStream("appResources/img/GESS.png");
you use like this
InputStream instream=Thread.currentThread().getContextClassLoader().getResourceAsStream("appResources/img/GESS.png");
Here are two utility methods I'm using for image loading.
public static Image getImage(final String pathAndFileName) {
try {
return Toolkit.getDefaultToolkit().getImage(getURL(pathAndFileName));
} catch (final Exception e) {
//logger.error(e);
return null;
}
}
public static URL getURL(final String pathAndFileName) {
return Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
}
I am trying to create an app in java using OpenCV to grab videostream from web service which is a camera system with couple of cameras and a recording device.
I have found the address "rtsp://login:pass#IP address:Port/cam/realmonitor?channel=1&subtype=0" for accessing the camera on channel 1.
For opening camera stream I have used this code (curently it catches a local usb camera):
VideoCapture cap;
Mat2Image mat2Img = new Mat2Image();
public VideoGrabber(){
cap = new VideoCapture(0);
try {
System.out.println("Sleeping..");
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Camera on..");
cap.open("0");
if(!cap.isOpened()){
System.out.println("Camera Error");
}
else{
System.out.println("Camera OK?");
}
}
After grabbing the video stream I put it into a JFrame.
I think I should put the video streaming service address in cap.open( ... ) but using rtsp://login:pass#http://192.168.1.14:8006/cam/realmonitor?channel=1&subtype=0 gave me "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0".
Please help,
EDIT
I have found out that rtsp://login:pass#http://192.168.1.14:554/cam/realmonitor?channel=1&subtype=0 works in vlc but still no luck in opencv.
EDIT #2
Ok. After playing with vlcl, gstreamer and most of the popular solutions it just started working. I don't know if it wasn't bad rtsp address after all. Code:
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//load the library of opencv
}
VideoCapture cap;
Mat2Image mat2Img = new Mat2Image();
Mat matFilter = new Mat();
public VideoGrabber(){
cap = new VideoCapture();
try {
System.out.println("Sleeping..");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Camera on..");
cap.open("rtsp://login:pass#192.168.1.14:554/cam/realmonitor?channel=1&subtype=0");
if(!cap.isOpened()){
System.out.println("Camera Error");
}
else{
System.out.println("Camera OK?");
}
}
Answering my question and for Fouad I post the working code:
I am guessing the answer was loading ffmpeg dll.
//all the imports
public class App {
static {
String path = null;
try {
//I have copied dlls from opencv folder to my project folder
path = "E:\\JAVA Projects\\OpenCv\\RTSP Example\\libraries";
System.load(path+"\\opencv_java310.dll");
System.load(path+"\\opencv_ffmpeg310_64.dll");
} catch (UnsatisfiedLinkError e) {
System.out.println("Error loading libs");
}
}
public static void main(String[] args) {
App app = new App();
//Address can be different. Check your cameras manual. :554 a standard RTSP port for cameras but it can be different
String addressString = "rtsp://login:password#192.168.1.14:554/cam/realmonitor?channel=11&subtype=0";
Mat mat = new Mat();
VideoCapture capturedVideo = new VideoCapture();
boolean isOpened = capturedVideo.open(addressString);
app.openRTSP(isOpened, capturedVideo, mat);
}
public void openRTSP(boolean isOpened, VideoCapture capturedVideo, Mat cameraMat) {
if (isOpened) {
boolean tempBool = capturedVideo.read(cameraMat);
System.out.println("VideoCapture returned mat? "+tempBool);
if (!cameraMat.empty()) {
System.out.println("Print image size: "+cameraMat.size());
//processing image captured in cameraMat object
} else {
System.out.println("Mat is empty.");
}
} else {
System.out.println("Camera connection problem. Check addressString");
}
}
}
I'm currently trying to write an automation code in java. In order to do that, I capture my screen and loop through it to find something I need to clic.
While working accordingly on my windows XP computer (I do get a correct screen-shot and thus can loop through it), on my windows 8.1 Machine I only get a black one.
Here is a part of my (faulty?) code.
public class NewClass {
static final Dimension dim_D = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
static final int i_SCREEN_WIDTH = (int) dim_D.getWidth();
static final int i_SCREEN_HEIGHT = (int) dim_D.getHeight();
public static void main(String[] args) {
try {
Robot robot = new Robot();
try {
BufferedImage biCapturedScreen = robot.createScreenCapture(new Rectangle(0, 0, i_SCREEN_WIDTH, i_SCREEN_HEIGHT));
ImageIO.write(biCapturedScreen, "PNG", new File("D:\\tempShot.png"));
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (AWTException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
As I get a PNG 1920x1080 black image, I guess this is not a writing permission problem.
Any idea why this isn't working on my 8.1 machine?
Thanks!
Thomas B.