Images and Sound Won't Load at the Same Time (Java) - java

I'm making a game which is using sound and images. The weirdest thing is happening. When I load sound, my image won't appear. However, when I don't load my sound, my image does appear. Here is my code:
package com.gbp.chucknorris;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Title extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
public Thread thread;
private BufferedImage logo;
private Clip clip, titleClip;
public Title() {
super();
loadSound();
loadImages();
bind();
setBackground(Color.WHITE);
}
private void loadSound() {
File f = new File("res/sounds/title.wav");
AudioInputStream stream = null;
try {
stream = AudioSystem.getAudioInputStream(f);
} catch (UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
}
DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
try {
clip = (Clip) AudioSystem.getLine(info);
} catch (LineUnavailableException e) {
e.printStackTrace();
}
try {
clip.open(stream);
} catch (LineUnavailableException | IOException e) {
e.printStackTrace();
}
File title = new File("res/sounds/theme.wav");
AudioInputStream titleStream = null;
try {
titleStream = AudioSystem.getAudioInputStream(title);
} catch (UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
}
DataLine.Info titleInfo = new DataLine.Info(Clip.class, titleStream.getFormat());
try {
titleClip = (Clip) AudioSystem.getLine(titleInfo);
titleClip.open(titleStream);
} catch (LineUnavailableException | IOException e) {
e.printStackTrace();
}
titleClip.loop(Clip.LOOP_CONTINUOUSLY);
}
private void bind() {
InputMap im = getInputMap();
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke("DOWN"), "down");
am.put("down", new AbstractAction() {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
}
});
}
private void loadImages() {
try {
logo = ImageIO.read(new File("res/pics/MenuPanel.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
while (true) {
repaint();
}
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(logo, 0, 0, null);
}
}
Thanks in advance!

You need to learn to use background threading such as a SwingWorker so as not to tie up the event thread, the EDT, when loading and playing sounds. You can read up on how to use this here: Lesson: Concurrency in Swing.
Edit: I wouldn't recommend doing this:
#Override
public void run() {
while (true) {
repaint();
}
}

Related

I am using ffmpeg java library to convert captured screenshots to video. Video output is blurry

I am using ffmpeg java library to convert captured screenshots to video. Video which is generated as output is blurry.
I am using bit rate as 9000, frames per sec as 25 and video size as that of desktop screen size.
Any suggestions on how to solve this issue.
P.S. I cannot use ffmpeg.exe and command line due to certain restrictions and hence I am opting for ffmpeg java library.
Any suggestions on the issue or suggestions on any better approach will be helpful.
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.OpenCVFrameConverter;
public class ScreenRecorder{
public static boolean videoComplete=false;
public static String inputImageDir="inputImgFolder"+File.separator;
public static String inputImgExt="png";
public static String outputVideo="recording.mp4";
public static int counter=0;
public static int imgProcessed=0;
public static FFmpegFrameRecorder recorder=null;
public static int videoWidth=1920;
public static int videoHeight=1080;
public static int videoFrameRate=3;
public static int videoQuality=0; // 0 is the max quality
public static int videoBitRate=9000;
public static String videoFormat="mp4";
public static int videoCodec=avcodec.AV_CODEC_ID_MPEG4;
public static Thread t1=null;
public static Thread t2=null;
public static JFrame frame=null;
public static boolean isRegionSelected=false;
public static int c1=0;
public static int c2=0;
public static int c3=0;
public static int c4=0;
public static void main(String[] args) {
try {
if(getRecorder()==null)
{
System.out.println("Cannot make recorder object, Exiting program");
System.exit(0);
}
if(getRobot()==null)
{
System.out.println("Cannot make robot object, Exiting program");
System.exit(0);
}
File scanFolder=new File(inputImageDir);
scanFolder.delete();
scanFolder.mkdirs();
createGUI();
} catch (Exception e) {
System.out.println("Exception in program "+e.getMessage());
}
}
public static void createGUI()
{
frame=new JFrame("Screen Recorder");
JButton b1=new JButton("Select Region for Recording");
JButton b2=new JButton("Start Recording");
JButton b3=new JButton("Stop Recording");
JLabel l1=new JLabel("<html><br/>If you dont select a region then full screen recording <br/> will be made when you click on Start Recording</html>");
l1.setFont (l1.getFont ().deriveFont (20.0f));
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
JOptionPane.showMessageDialog(frame, "A new window will open. Use your mouse to select the region you like to record");
new CropRegion().getImage();
} catch (Exception e1) {
// TODO Auto-generated catch block
System.out.println("Issue while trying to call the module to crop region");
e1.printStackTrace();
}
}
});
b2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
counter=0;
startRecording();
}
});
b3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
stopRecording();
System.out.print("Exiting...");
System.exit(0);
}
});
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(l1);
frame.setLayout(new FlowLayout(0));
frame.setVisible(true);
frame.setSize(1000, 170);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void startRecording()
{
t1=new Thread()
{
public void run() {
try {
takeScreenshot(getRobot());
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Cannot make robot object, Exiting program "+e.getMessage());
System.out.println("Cannot make robot object, Exiting program "+e.getMessage());
System.exit(0);
}
}
};
t2=new Thread()
{
public void run() {
prepareVideo();
}
};
t1.start();
t2.start();
System.out.println("Started recording at "+new Date());
}
public static Robot getRobot() throws Exception
{
Robot r=null;
try {
r = new Robot();
return r;
} catch (AWTException e) {
JOptionPane.showMessageDialog(frame, "Issue while initiating Robot object "+e.getMessage());
System.out.println("Issue while initiating Robot object "+e.getMessage());
throw new Exception("Issue while initiating Robot object");
}
}
public static void takeScreenshot(Robot r)
{
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rec=new Rectangle(size);
if(isRegionSelected)
{
rec=new Rectangle(c1, c2, c3-c1, c4-c2);
}
while(!videoComplete)
{
counter++;
BufferedImage img = r.createScreenCapture(rec);
try {
ImageIO.write(img, inputImgExt, new File(inputImageDir+counter+"."+inputImgExt));
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "Got an issue while writing the screenshot to disk "+e.getMessage());
System.out.println("Got an issue while writing the screenshot to disk "+e.getMessage());
counter--;
}
}
}
public static void prepareVideo()
{
File scanFolder=new File(inputImageDir);
while(!videoComplete)
{
File[] inputFiles=scanFolder.listFiles();
try {
getRobot().delay(500);
} catch (Exception e) {
}
//for(int i=0;i<scanFolder.list().length;i++)
for(int i=0;i<inputFiles.length;i++)
{
//imgProcessed++;
addImageToVideo(inputFiles[i].getAbsolutePath());
//String imgToAdd=scanFolder.getAbsolutePath()+File.separator+imgProcessed+"."+inputImgExt;
//addImageToVideo(imgToAdd);
//new File(imgToAdd).delete();
inputFiles[i].delete();
}
}
File[] inputFiles=scanFolder.listFiles();
for(int i=0;i<inputFiles.length;i++)
{
addImageToVideo(inputFiles[i].getAbsolutePath());
inputFiles[i].delete();
}
}
public static FFmpegFrameRecorder getRecorder() throws Exception
{
if(recorder!=null)
{
return recorder;
}
recorder = new FFmpegFrameRecorder(outputVideo,videoWidth,videoHeight);
try
{
recorder.setFrameRate(videoFrameRate);
recorder.setVideoCodec(videoCodec);
recorder.setVideoBitrate(videoBitRate);
recorder.setFormat(videoFormat);
recorder.setVideoQuality(videoQuality); // maximum quality
recorder.start();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(frame, "Exception while starting the recorder object "+e.getMessage());
System.out.println("Exception while starting the recorder object "+e.getMessage());
throw new Exception("Unable to start recorder");
}
return recorder;
}
public static OpenCVFrameConverter.ToIplImage getFrameConverter()
{
OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
return grabberConverter;
}
public static void addImageToVideo(String imgPath)
{
try {
getRecorder().record(getFrameConverter().convert(cvLoadImage(imgPath)));
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Exception while adding image to video "+e.getMessage());
System.out.println("Exception while adding image to video "+e.getMessage());
}
}
public static void stopRecording()
{
try {
videoComplete=true;
System.out.println("Stopping recording at "+new Date());
t1.join();
System.out.println("Screenshot thread complete");
t2.join();
System.out.println("Video maker thread complete");
getRecorder().stop();
System.out.println("Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());
JOptionPane.showMessageDialog(frame, "Recording has been saved successfully at "+new File(outputVideo).getAbsolutePath());
} catch (Exception e) {
System.out.println("Exception while stopping the recorder "+e.getMessage());
}
}
}
Imagepanel.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
class ImagePanel
extends JPanel
{
private Image img;
public ImagePanel(String img)
{
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img)
{
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g)
{
g.drawImage(this.img, 0, 0, null);
}
}
CropRegion.java
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class CropRegion implements MouseListener,
MouseMotionListener {
int drag_status = 0;
int c1;
int c2;
int c3;
int c4;
JFrame frame=null;
static int counter=0;
JLabel background=null;
public void getImage() throws AWTException, IOException, InterruptedException {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
ImagePanel panel = new ImagePanel(img);
frame=new JFrame();
frame.add(panel);
frame.setLocation(0, 0);
frame.setSize(size);
frame.setLayout(new FlowLayout());
frame.setUndecorated(true);
frame.setVisible(true);
frame.addMouseListener(this);
frame.addMouseMotionListener(this);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void draggedScreen() throws Exception {
ScreenRecorder.c1=c1;
ScreenRecorder.c2=c2;
ScreenRecorder.c3=c3;
ScreenRecorder.c4=c4;
ScreenRecorder.isRegionSelected=true;
JOptionPane.showMessageDialog(frame, "Region Selected.Please click on Start Recording button to record the selected region.");
frame.dispose();
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
paint();
this.c1 = arg0.getX();
this.c2 = arg0.getY();
}
public void mouseReleased(MouseEvent arg0) {
paint();
if (this.drag_status == 1) {
this.c3 = arg0.getX();
this.c4 = arg0.getY();
try {
draggedScreen();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void mouseDragged(MouseEvent arg0) {
paint();
this.drag_status = 1;
this.c3 = arg0.getX();
this.c4 = arg0.getY();
}
public void mouseMoved(MouseEvent arg0) {
}
public void paint() {
Graphics g = frame.getGraphics();
frame.repaint();
int w = this.c1 - this.c3;
int h = this.c2 - this.c4;
w *= -1;
h *= -1;
if (w < 0) {
w *= -1;
}
g.drawRect(this.c1, this.c2, w, h);
}
}

Application is stopping because Memory usage is too high

Every 50 Miliseconds I am taking a Screenshot and sending it to another Computer which is showing the Screenshot. I am mirroring the Desktop. Now the problem is the Program is stopping because the Memory usage is getting too high. My Question is how can I "clear" the RAM - I already googled and found out that I should use the Garbage Collector but this isn't working. Here is my code:
CLIENT:
Client.java
import java.awt.AWTException;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ConnectException;
import java.net.Socket;
import java.net.SocketException;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Client implements Runnable {
private Socket socket;
private ObjectInputStream din;
private static ObjectOutputStream dout;
public Client(String ip, int port) {
try {
socket = new Socket(ip, port);
din = new ObjectInputStream(socket.getInputStream());
dout = new ObjectOutputStream(socket.getOutputStream());
} catch (ConnectException e) {
JOptionPane.showMessageDialog(null, "Server is offline");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
Thread.sleep(50);
Robot robot = new Robot();
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, "jpeg", new File("C:\\Windows\\Temp\\screenshot.jpeg"));
} catch (IOException | AWTException | InterruptedException e) {
e.printStackTrace();
}
ImageIcon ii = new ImageIcon("C:\\Windows\\Temp\\screenshot.jpeg");
send(new Obj(ii));;
}
}
public static void send(Obj obj) {
try {
dout.writeObject(obj);
dout.flush();
} catch (SocketException e) {
JOptionPane.showMessageDialog(null, "Connection closed");
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
dout.close();
din.close();
socket.close();
} catch (IOException e) {
}
}
}
Main.java
public class Main {
public static void main(String[] args) {
// Connect to the Server
Client client = new Client("localhost", 9785);
// Runs a new Client
Thread clientThread = new Thread(client);
clientThread.start();
}
}
Obj.java
package ch.nyp.blj;
import java.io.Serializable;
import javax.swing.ImageIcon;
public class Obj implements Serializable {
private ImageIcon img;
public Obj(ImageIcon img) {
super();
this.img = img;
}
public ImageIcon getImg() {
return img;
}
public void setImg(ImageIcon img) {
this.img = img;
}
}
SERVER:
Main.java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
GUI.open();
try {
ServerSocket s = new ServerSocket(9785);
System.out.println("Server Online");
while (true) {
Socket client = s.accept();
if (client != null) {
ClientThread ct = new ClientThread(client);
new Thread(ct).start();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Obj.java
import java.io.Serializable;
import javax.swing.ImageIcon;
public class Obj implements Serializable {
private ImageIcon img;
public Obj(ImageIcon img) {
super();
this.img = img;
}
public ImageIcon getImg() {
return img;
}
public void setImg(ImageIcon img) {
this.img = img;
}
}
GUI.java
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
public class GUI {
public static JLabel img;
private JFrame frame;
public static Dimension d = new Dimension(400, 400);
/**
* Launch the application.
*/
public static void open() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setSize(d.width, d.height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
img = new JLabel("");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(img, GroupLayout.DEFAULT_SIZE, 1924, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(img, GroupLayout.DEFAULT_SIZE, 1181, Short.MAX_VALUE)
);
frame.getContentPane().setLayout(groupLayout);
}
}
ClientThread.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
public class ClientThread implements Runnable {
private Socket socket;
private ObjectOutputStream dout;
private ObjectInputStream din;
private static Obj input = null;
ClientThread(Socket socket) {
this.socket = socket;
try {
this.dout = new ObjectOutputStream(socket.getOutputStream());
this.din = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
#Override
public void run() {
try {
while (true) {
try {
input = (Obj) din.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
GUI.img.setIcon(input.getImg());
System.out.println(input.getImg());
}
} catch (SocketException e) {
Thread.currentThread().interrupt();
} catch (IOException e) {
}
}
}
Exception after some seconds:
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
at java.base/java.lang.reflect.Array.newArray(Native Method)
at java.base/java.lang.reflect.Array.newInstance(Unknown Source)
at java.base/java.io.ObjectInputStream.readArray(Unknown Source)
at java.base/java.io.ObjectInputStream.readObject0(Unknown Source)
at java.base/java.io.ObjectInputStream.readObject(Unknown Source)
at java.desktop/javax.swing.ImageIcon.readObject(Unknown Source)
at java.base/jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.base/java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectInputStream.readObject0(Unknown Source)
at java.base/java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.base/java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.base/java.io.ObjectInputStream.readObject0(Unknown Source)
at java.base/java.io.ObjectInputStream.readObject(Unknown Source)
at ch.nyp.blj.ClientThread.run(ClientThread.java:31)
at java.base/java.lang.Thread.run(Unknown Source)

NotFoundException even when a barcode is in the picture

I am trying to make a simple barcode scanner project for fun. And I've run into a slight problem. I am using zXing and Webcam Capture for this.
Even if a Barcode is present in the picture, Java keeps telling me none is found through the NotFoundException. I look for a frame every time webcamImageObtained is run (which I assume is every frame?) and then I look for a barcode in the frame that I captured.
I took this picture with that webcam (Ironically using the code hah):
When I hover over this barcode it reports about 30 images per second and otherwise about 7-8 when it looks at me from my screen (if that means anything).
Whenever I find a code, I want to add it to a JList (not accounting for duplicates and the likes yet).
I call this code every time webcamImageObtained(WebcamEvent we) fires:
#Override
public void webcamImageObtained(WebcamEvent we) {
BufferedImage myImage;
try {
myImage = webcam.getImage();
LuminanceSource source = new BufferedImageLuminanceSource(myImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = reader.decode(bitmap);
DefaultListModel dlm = (DefaultListModel) list.getModel();
dlm.addElement(result.toString());
list.setModel(dlm);
} catch (NotFoundException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (ChecksumException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (FormatException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
Here is the entire class:
package sandbox_webcam;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamDiscoveryEvent;
import com.github.sarxos.webcam.WebcamDiscoveryListener;
import com.github.sarxos.webcam.WebcamEvent;
import com.github.sarxos.webcam.WebcamListener;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamPicker;
import com.github.sarxos.webcam.WebcamResolution;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
public class AdvancedWebcamPanelExample extends JFrame implements Runnable, WebcamListener, WindowListener, UncaughtExceptionHandler, ItemListener, WebcamDiscoveryListener {
private Webcam webcam = null;
private WebcamPanel panel = null;
private WebcamPicker picker = null;
private JButton button = null;
private JList list = null;
private ActionListener buttonListener = null;
private com.google.zxing.Reader reader = new com.google.zxing.MultiFormatReader();
#Override
public void run() {
Webcam.addDiscoveryListener(this);
setTitle("Java Webcam Capture POC");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
addWindowListener(this);
picker = new WebcamPicker();
picker.addItemListener(this);
webcam = picker.getSelectedWebcam();
if (webcam == null) {
System.out.println("No webcams found...");
System.exit(1);
}
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.addWebcamListener(AdvancedWebcamPanelExample.this);
panel = new WebcamPanel(webcam, false);
panel.setFPSDisplayed(true);
buttonListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (webcam != null) {
BufferedImage image = webcam.getImage();
JFileChooser filechooser = new JFileChooser();
int saveValue = filechooser.showDialog(button, "Save");
if (saveValue == JFileChooser.APPROVE_OPTION) {
try {
File f = filechooser.getSelectedFile();
ImageIO.write(image, "png", new File(f.getAbsolutePath() + ".png"));
System.out.println("Picture saved at: " + f.getAbsolutePath());
} catch (IOException ex) {
System.err.println("Failed to save the picture!");
ex.printStackTrace();
}
}
} else {
System.err.println("no webcam found to take a picture");
}
}
};
button = new JButton("Snap a Picture!");
button.addActionListener(buttonListener);
list = new JList();
list.setMinimumSize(new Dimension(200,this.getHeight()));
add(picker, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
add(list, BorderLayout.EAST);
pack();
setVisible(true);
Thread t = new Thread() {
#Override
public void run() {
panel.start();
}
};
t.setName("example-starter");
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.start();
}
#Override
public void webcamOpen(WebcamEvent we) {
System.out.println("webcam open");
}
#Override
public void webcamClosed(WebcamEvent we) {
System.out.println("webcam closed");
}
#Override
public void webcamDisposed(WebcamEvent we) {
System.out.println("webcam disposed");
}
#Override
public void webcamImageObtained(WebcamEvent we) {
BufferedImage myImage;
try {
myImage = webcam.getImage();
LuminanceSource source = new BufferedImageLuminanceSource(myImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = reader.decode(bitmap);
DefaultListModel dlm = (DefaultListModel) list.getModel();
dlm.addElement(result.toString());
list.setModel(dlm);
} catch (NotFoundException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (ChecksumException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
} catch (FormatException ex) {
Logger.getLogger(AdvancedWebcamPanelExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void windowOpened(WindowEvent e) {
// do nothing
}
#Override
public void windowClosing(WindowEvent e) {
// do nothing
}
#Override
public void windowClosed(WindowEvent e) {
webcam.close();
}
#Override
public void windowIconified(WindowEvent e) {
System.out.println("webcam viewer paused");
panel.pause();
}
#Override
public void windowDeiconified(WindowEvent e) {
System.out.println("webcam viewer resumed");
panel.resume();
}
#Override
public void windowActivated(WindowEvent e) {
// do nothing
}
#Override
public void windowDeactivated(WindowEvent e) {
// do nothing
}
#Override
public void uncaughtException(Thread t, Throwable e) {
System.err.println(String.format("Exception in thread #s", t.getName()));
e.printStackTrace();
}
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getItem() != webcam) {
if (webcam != null) {
panel.stop();
remove(panel);
webcam.removeWebcamListener(this);
webcam.close();
webcam = (Webcam) e.getItem();
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.addWebcamListener(this);
System.out.println("selected " + webcam.getName());
panel = new WebcamPanel(webcam, false);
panel.setFPSDisplayed(true);
add(panel, BorderLayout.CENTER);
pack();
Thread t = new Thread() {
#Override
public void run() {
panel.start();
}
};
t.setName("example-stopper");
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.start();
}
}
}
#Override
public void webcamFound(WebcamDiscoveryEvent event) {
if (picker != null) {
picker.addItem(event.getWebcam());
}
}
#Override
public void webcamGone(WebcamDiscoveryEvent event) {
if (picker != null) {
picker.removeItem(event.getWebcam());
}
}
}
Am I missing something about how this library scans for a barcode?
EDIT
Not sure this helps much..
Mar 02, 2015 10:04:34 PM sandbox_webcam.AdvancedWebcamPanelExample webcamImageObtained
SEVERE: null
com.google.zxing.NotFoundException
Throws exception here:
Result result = reader.decode(bitmap);
There is a different question which has some available answers: Android zxing NotFoundException
As James said, it is a good idea to try with bar codes on different media (paper/screen) if it is not working, and in different circumstances and lighting conditions. Particularly ensure that you have enough light, and that the FPS of the camera is high enough while it is pointed at the barcode.
For debugging, one could also convert the BinaryImage back into a viewable format and check whether the barcode is actually visible after conversion to black-and-white.

Captured webcam image not refreshing and max size too big to show

I am having some problems with my program. I have a GUI that shows a live image from a webcam (using [jvacv][1]) in one side, and the captured image in the other. To capture the image, I have a button. One problem is that the captured image is refreshing only if I close and open the program again. The other is that I want to capture a 1080p image from webcam, but live image at 640x480.
Here is the code:
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.FrameGrabber.Exception;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
public class Final
{
private VideoPanel videoPanel = new VideoPanel();
private ImagePanel imagePanel = new ImagePanel();
private JButton jbtCapture = new JButton("Captura");
private JRadioButton jbtAutoCap = new JRadioButton("Captura Automatica");
private FrameGrabber vision;
private BufferedImage image;
private IplImage gimage;
public class VideoPanel extends JPanel
{
public VideoPanel()
{
vision = new OpenCVFrameGrabber(0);
try
{
vision.start();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
try {
image = vision.grab().getBufferedImage();
if (image != null)
{
g.drawImage(image, 0, 0, 640, 480, null);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
class ImagePanel extends JPanel
{
private BufferedImage image;
public ImagePanel()
{
try {
image = ImageIO.read(new File("image001.bmp"));
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(image != null)
{
g.drawImage(image,5,0,640,480, null);
}
}
}
private void displayGUI()
{
JFrame janela = new JFrame();
JPanel jpButton = new JPanel();
jpButton.setLayout(null);
jbtCapture.setBounds(145,0,110,30);
jpButton.add(jbtCapture);
jbtAutoCap.setBounds(0, 5, 140, 23);
jpButton.add(jbtAutoCap);
janela.setLayout(null);
videoPanel.setBounds(5, 5, 640, 480);
janela.add(videoPanel);
imagePanel.setBounds(705,5,640,480);
janela.add(imagePanel);
jpButton.setBounds(5, 500, 670, 40);
janela.add(jpButton);
janela.setSize(1366,730);
janela.setVisible(true);
jbtCapture.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
gimage = vision.grab();
cvSaveImage("image001.bmp", gimage);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
// TODO Auto-generated method stub
new Final().displayGUI();
}
});
}
}

Serialization in java, Reinflating the object in an event?

I am trying to learn more about java. This program is an attempt to understand events as well as serialization. What i am attempting to do is flatten an object when the user closes the JFrame and re-inflate it when the program is started. I know i can create the serialized file but having it take effect again isn't working. Any help in the right direction would be wonderful. Thank you in advance.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.Timer;
import java.io.*;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class tempusFugit extends JFrame implements ActionListener, Serializable, WindowListener
{
String fileN = "tf.txt";
public int ToL = 0;
String outT = Integer.toString(ToL);
JLabel jl = new JLabel(outT);
FileOutputStream fos = null;
ObjectOutputStream out = null;
public tempusFugit()
{
Timer timer = new Timer(1000, this);
setBounds(250, 250, 250, 190);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT) );
setVisible(true);
add(jl);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
++ToL;
outT = Integer.toString(ToL);
jl.setText(outT);
validate();
repaint();
}
public static void main(String[] args)
{
tempusFugit tf = new tempusFugit();
tf.addWindowListener( tf );
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
try
{
fos = new FileOutputStream(fileN);
out = new ObjectOutputStream(fos);
out.writeObject(this);
out.close();
}
catch(IOException ex)
{
}
}
public void windowOpened(WindowEvent e)
{
try
{
tempusFugit tf = new tempusFugit();
FileInputStream fis = new FileInputStream(fileN);
ObjectInputStream in = new ObjectInputStream(fis);
tf = (tempusFugit)in.readObject();
this.ToL = tf.ToL;
}
catch(IOException ex)
{
}
catch(ClassNotFoundException ce)
{
}
}
}
I assume i'm trying to recreate the object at the wrong time. Even though the object is serialized correctly i can not access it again with the windowOpened function. Do i need to try to use
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
somehow?
What i end up with is an error saying i am trying to access a Final object (i assume my this). I find that very odd that i cant repopulate my current 'this' with another similar object.
Am i way off base?
Again thank you for your time.
Note that there are much simpler ways to do this than serializing the frame, and much better things to serialize than the frame itself.
See What is the best practice for setting JFrame locations in Java? for an example of storing the location and size of a frame. It would be trivial to adapt that to store the count.
But here is an attempt based on your code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.Timer;
import java.io.*;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class tempusFugit extends JFrame implements ActionListener, Serializable, WindowListener
{
String fileN = "tf.txt";
public int ToL = 0;
JLabel jl = new JLabel("" + ToL);
public tempusFugit()
{
Timer timer = new Timer(1000, this);
setBounds(250, 250, 250, 190);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT) );
setVisible(true);
add(jl);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
++ToL;
jl.setText("" + ToL);
validate();
repaint();
}
public static void main(String[] args)
{
tempusFugit tf = new tempusFugit();
tf.addWindowListener( tf );
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e)
{
try
{
FileOutputStream fos = new FileOutputStream(fileN);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(this);
out.flush();
out.close();
setVisible(false);
System.exit(0);
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null, ex);
System.exit(1);
}
}
public void windowOpened(WindowEvent e)
{
try
{
tempusFugit tf;// = new tempusFugit();
FileInputStream fis = new FileInputStream(fileN);
ObjectInputStream in = new ObjectInputStream(fis);
tf = (tempusFugit)in.readObject();
this.ToL = tf.ToL;
//tf.setVisible(false);
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException ce)
{
ce.printStackTrace();
}
}
}

Categories