I have a requirement to capture photos using system webcam. For this, I'm using webcam capture library. It contained three jar files. I wanted to package every thing including dependency jars in single jar application.
So, I sorted to unpack the dependent jar files place them in the src folder of netbeans project.
So, I extracted them. Two of them start with the same package name org, so I extracted and merged them. Important third package starts with com package. I've placed these three directories in src folder of my netbeans project.
Now the problem is that netbeans, says that package doesn't exist. But, the same project when compiled from command line in windows. It compiled and ran successfully, everything was working fine.
But, when I added the problematic library as a jar to the project, it worked fine, only extracting and placing it in src folder causing the problem.
Why is this error occurring and why in netbeans only, how to resolve this problem?
My code:
package screen;
import com.github.sarxos.webcam.*; **//Error here**
import com.github.sarxos.webcam.log.*; **// Error here**
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Util extends TimerTask{
private String filePath;
private String type;
private Timer timer;
private Webcam webcam;
public Util(String path,String type){
this.timer=new Timer("Capture Timer");
this.filePath=path;
this.type=type;
this.webcam=Webcam.getDefault();
}
public void capture() throws IOException{
if(webcam!=null){
webcam.open();
BufferedImage image=webcam.getImage();
Date date=new Date();
ImageIO.write(image,type,new File(filePath+"_"+date.toString().replace(' ','_').replace(':','-')+"."+type.toLowerCase()));
webcam.close();
}
else{
webcam=Webcam.getDefault();
}
}
public void startCapturing(int period){
this.timer.schedule(this,0,period);
}
public void stopCapturing(){
timer.cancel();
}
public void run(){
try{
capture();
System.out.println("Doing");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
Util u=new Util("n082430_","PNG");
u.startCapturing(60000);
}
}
Related
I want to create a Java class that I use to export images. This class will be exported in .JAR file which will allow me to integrate it directly into another project by passing as input parameter the name of the file and in return I would have this image in PNG format.
That's what I tried:
package militarySymbolsLibrary;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class MainEntry {
public static void main(String agrs[]) {
}
public static BufferedImage generateImage(String symbolCode) {
File imageFile = new File("/pictures/SSGPU-------.png");
BufferedImage image;
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
image = null;
e.printStackTrace();
}
return image;
}
}
Pictures are stocked in folder pictures like this treeview below:
src
MainEntry.java
pictures
SSGPU-------.png
Then I export this class in .JAR file, I add it to my other program, I pass a parameter (picture's name except for this test) and normally my class return PNG file. But I have an error telling me that the file path is not the rigth one.
How can I solve this problem?
Thank you
I have the following package hierarchy:
base--images--img.png
|
--code--Foo.java
|
--Base.java
That is, I have a package base which has 2 subpackages, base.images and base.code with respective files in each. Base.java just calls a method in Foo.
All of this gets bundled into base.jar when I build.
The problem: How can Foo.java load img.png no matter where the jar is located or run from?
Some things I've tried:
new File("../images/img.png")
Foo.class.getResource("../images/img.png")
Neither of these work because the "../images/img.png" is appended to the path the JAR is run from, not the path of the class.
MWE:
In Foo.java:
package base.code;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class Foo {
public static void main(String[] args) {
URL url = Foo.class.getResource("/graphs/1.png");
try {
BufferedImage img = ImageIO.read(url);
} catch (IOException e) {
System.err.println("Image not found!");
}
}
}
In Base.java:
package base;
public class Base {
public static void main(String[] args)
Foo.main(args);
}
}
Resources are on the class path, never use a File. They might be stored in the jar (zip format).
The solution is simply to use an absolute path. That is more clear than a class package's relative path.
Foo.class.getResource("/base/images/img.png")
I've got an issue with my .jar file. It runs fine in Eclipse but as soon as I export it, it won't open. I've checked the manifest file and it looks like it's okay.
I've tried exporting it as a runnable jar, as well as just using the jar builder. Nothing worked.
I've tried to run it in command prompt and it says it can't access the jar file... I've searched around a while on here and haven't found an answer yet.
I'm not sure what I'm doing wrong. The only thing I can think of is I'm not getting my images correctly.
I'm using .png files for the program's sprites and here's an example of how I get them for my program.
This code begins the building of a level from a .png file.
public class SpawnLevel extends Level{
public SpawnLevel(String path) {
super(path);
}
protected void loadLevel(String path){
try{
System.out.println("classpath is: " + System.getProperty("java.class.path"));
BufferedImage image = ImageIO.read(SpawnLevel.class.getResource(path));
int w = width = image.getWidth();
int h = height= image.getHeight();
tiles = new int[w*h];
image.getRGB(0,0,w,h,tiles,0,w);
}catch(IOException e){
e.printStackTrace();
System.out.println("EXEPTION FOUND!!! Could not load the level file!");
}
}
protected void generateLevel(){
System.out.println("Tiles: " + tiles[0]);
}
}
I've made one other .jar before for another program and didn't have a problem. Any help would be greatly appreciated.
If it helps, I used this code to display the resource folder path information.
System.out.println("classpath is: " + System.getProperty("java.class.path"));
Here's what my current path for my resources folder looks like. Before I export it from eclipse.
classpath is: C:\Users\name\workspace\Rpg_Game\bin;C:\Users\name\workspace\Rpg_Game\res
After I export to .jar
classpath is: 2ndGameTest.jar
If your images are in your resources package in the src The path you should be using for getResource() is something like
class.getResource("/resources/levels/level1.png")
UPDATE with test program
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TestImage {
public static void main(String[] args) throws IOException {
Image image = ImageIO.read(TestImage.class.getResource("/resources/images/image.png"));
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
JOptionPane.showMessageDialog(null, label);
}
}
I am wanting to make a simple java applicaiton to play video. I want it to play mpeg4 and mov formats in particular. JMF is what I started with and I have a lovely working example. However, there is no support for mov or mpeg4 formats. I've looked at Xuggler but can't see a SIMPLE way to get it working. VLCJ seemed easy - I downloaded the jar files and attached them to my project (vlcj-2.1.0.jar, jna-3.4.0.jar, platform-3.4.0.jar, vlcj-2.1.0.jar)). I got the sample code and adapted it (below). But when I run the code, I get a java.lang.NullPointerException exception. I've tried adjusting the number and direciton of the slashes (forward and backward) in the filename. Nothing seems to work. Please could you help???
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JFileChooser;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import java.lang.Object;
import uk.co.caprica.vlcj.mrl.FileMrl;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class TestPlayer {
private final JFrame frame;
private EmbeddedMediaPlayerComponent mediaPlayer;
public static void loadLibs(){
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "C:/Program Files/VideoLAN/VLC/"
);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
public static void main(final String[] args){
loadLibs();
final String mrl = "file://C:/Test.mov";
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestPlayer().run(mrl);
}
});
}
public TestPlayer(){
frame = new JFrame("test VLCJ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100,100);
frame.setSize(600,400);
frame.setVisible(true);
}
private void run(String mrl){
System.out.println(mrl);
try{
mediaPlayer.getMediaPlayer().playMedia(mrl);
}catch(Exception e){
System.err.println(e.toString());
}
}
}
I'm using VLC version 2.0.2 and VLCJ 2.1.0 sources and JDK 1.7 on windows 32 bit. I hope it's something simple...
It looks like you are using mediaPlayerwithout ever initializing it, thus causing a NullPointerException in run().
Try initializing it in your constructor.
Hello this is Lonnie Ribordy,
I have a program i am trying to write and part of it uses a 3rd party api called JDom,
when i compile my program the it compiles perfectly fine.. but, when i try to run it i get the Exception in thread "main" java.lang.noClassDefFoundError org/jdom/input/SAXBuilder
my program is as below...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class COS extends JPanel implements ActionListener{
static JFrame f=new JFrame();
Image bgImage=null;
String message="";
public COS(){
try{
String xml="background.xml";
SAXBuilder builder=new SAXBuilder();
Document doc=builder.build(new File(xml));
Element root=null;
Element img=null;
String fimg=null;
try{
root=doc.getRootElement();
img=root.getChild("bgimage");
fimg=img.getText();
} catch(Exception e){
}
getFileImage(fimg);
} catch(Exception e){
message="File load failed: "+e.getMessage();
}
}
public void paintComponent(Graphics g){
if(bgImage!=null){
g.drawImage(bgImage,0,0,this);
}
else{
g.drawString(message,40,40);
}
}
public void getFileImage(String filein) throws IOException, InterruptedException{
FileInputStream in=new FileInputStream(filein);
byte[] b=new byte[in.available()];
in.read(b);
in.close();
bgImage=Toolkit.getDefaultToolkit().createImage(b);
MediaTracker mt=new MediaTracker(this);
mt.addImage(bgImage,0);
mt.waitForAll();
}
public void actionPerformed(ActionEvent e){
}
public static void main(String[] args){
COS newcos=new COS();
f.setSize(825,640);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);
newcos.setBounds(5,5,800,600);
f.setLocation(10,5);
f.getContentPane().add(newcos);
f.setVisible(true);
}
}
could anybody tell what's wrong?
i found out my problem, when i installed JDom into my java i forgot to include it in the jre lib/ext
and now everything works just like it is supposed to work, thankyou very much for the time you spent helping me
I believe the issue is that you have an old version of JDom in your classpath, and that is being loaded before the one you want.
Firstly, make sure that you don't have another version of JDom (besides the one you've downloaded) kicking about in your classpath. To find out where the
org.jdom.input.SAXBuilder
class is being loaded from download JWhich and use that to check where the class is being loaded from.
Secondly, if you're using maven check that another version of JDOM isn't including it as a dependency, to do this use the mvn dependency:tree command.