How include image in .jar file for exporting into .exe - image

i made this code:
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ExplaneImage {
public static void main( String[] args )
{
Image image = null;
try {
File sourceimage = new File
("C:\\...\\ExplaneImage\\im1.jpg");
image = ImageIO.read(sourceimage);
} catch (IOException e) {}
JFrame frame = new JFrame();
frame.setSize(496, 325);
JLabel label = new JLabel(new ImageIcon(image));
frame.add(label);
frame.setVisible(true);
}
}
I'll need export this first in .jar file, after in .exe file (I have found the program to convert it), but it doesn't work cause exportation was made from class and not from a package, and the picture is not included in class.
I would like to make a simple images viewer for export in .exe format (to use on computers that do not have the jdk and jre) , independent, also containing images.
Thanks a lot for your help

Related

Thumbnailator Image Rotation messes the image

I am trying to rotate images using Thumbnailator library. The code that I use is as shown below. It rotates the image or flips the image successfully but the color quality completely spoils. The input and output images are also shown.
package com.abk;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.filters.Flip;
import net.coobird.thumbnailator.filters.Rotation;
import net.coobird.thumbnailator.util.exif.ExifUtils;
import net.coobird.thumbnailator.util.exif.Orientation;
public class ImageAutoRotate {
public static void main(String[] args) {
try {
BufferedImage img = ImageIO.read(new File("314.jpg"));
BufferedImage newImg = Rotation.RIGHT_90_DEGREES.apply(img);
BufferedImage flipImg = Flip.HORIZONTAL.apply(img);
File outputfile = new File("314_2.jpg");
ImageIO.write(newImg, "jpg", outputfile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Input Image
Output Image
Resolved the issue by saving the image as a PNG image. The issue was actually caused because the image profile was being treated as CMYK while saving
The final code to resolve this issue is shown at this link
Had the same issue it is due to the ImageIO.write class. Following is the approach I used.
//Here imagebuffer is an array of bytes you can convert it from bufferedimage
InputStream fiStream = new ByteArrayInputStream(imageBuffer);
//Creating a file using byte array
FileUtils.writeByteArrayToFile(new File(path + "/webapps/northstar-
primefaces-portlet/images/member/" + event.getFile().getFileName()), imageBuffer);
//Creating an output stream using the created file
OutputStream out = new FileOutputStream(path + "/webapps/northstar-primefaces-portlet/images/member/" + event.getFile().getFileName());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(fiStream).scale(1).toOutputStream(outputStream);
//Now the outstream has the rotated image with the correct colours
Let me know if this helps.
Reference:
https://github.com/coobird/thumbnailator/issues/23

When Loading Image Using BufferdImage and ImageIO it Clears my Whole Frame and Does Not Display Image

I and making a program using basic GUI involving buttons, frames, and panels, and everything was fine until I tried to load an image from my project folder. When i add the line of code
try{
titleImage = ImageIO.read(new File("mouse_title_resize.png"));
}
catch(Exception e){}
After I run the program my whole frame just becomes blank whereas before I had some JButtons on it.All the code I had before the try-catch line worked perfectly fine and I tested to see that the only thing that breaks it is this line of code. I receive no errors or anything and I have the image in my project folder and it seems that the image loaded fine, except it wont show up on the frame, and everything else on the frame disappears. I just don't understand why it clears my whole frame when i load the image.
Here is the full code:
This is the class that extends JFrame
package mouse.click.game;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class MouseClickGame extends JFrame {
//Constants to define the frame width and height including borders
public final int FRAME_WIDTH = 600;
public final int FRAME_HEIGHT = 600;
//Dimension from Toolkit to be able to get width and height of screen
public Dimension sizeTool = Toolkit.getDefaultToolkit().getScreenSize();
//Using sizeTool to get width of screen
public double xResolution = sizeTool.getWidth();
//Using sizeTool to get height of screen
public double yResolution = sizeTool.getHeight();
//Creating a point object that is defined as the center of the screen
public Point middleOfScreen = new Point((int) (xResolution / 2) - (FRAME_WIDTH / 2), (int) (yResolution / 2) - (FRAME_HEIGHT / 2));
public MouseClickGame() {
super("WELCOME :D");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocation(middleOfScreen);
add(new MouseClickPanel());
}
public static void main(String[] args) {
//Calling constructor
MouseClickGame mainClickGame = new MouseClickGame();
}
}
And here is the class that extends JPanel (these are the only two classes in my project)
package mouse.click.game;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MouseClickPanel extends JPanel {
JButton buttonPlay = new JButton("Play");
JButton buttonContinue = new JButton("Continue");
JButton buttonOptions = new JButton("Options");
JButton buttonExit = new JButton("Exit");
BoxLayout boxLay = new BoxLayout(this, BoxLayout.Y_AXIS);
Dimension menuButtonSize = new Dimension(300, 30);
Dimension spacingBetweenButtons = new Dimension(0, 30);
BufferedImage titleImage;
public MouseClickPanel() {
try {
titleImage = ImageIO.read(new File("C:\\Users\\Justin\\Desktop\\mouse_title.png"));
} catch (IOException e) {
}
setLayout(boxLay);
add(Box.createVerticalGlue());
add(new JLabel(new ImageIcon(titleImage)));
//Adding glue to force buttons away from top of panel
add(Box.createVerticalGlue());
add(buttonPlay);
//Vertical spacing between buttons
add(Box.createRigidArea(spacingBetweenButtons));
add(buttonContinue);
add(Box.createRigidArea(spacingBetweenButtons));
add(buttonOptions);
add(Box.createRigidArea(spacingBetweenButtons));
add(buttonExit);
//Adding glue to force buttons away from bottom of panel
add(Box.createVerticalGlue());
//Aligning all buttons to centered horizontally
buttonPlay.setAlignmentX(Box.CENTER_ALIGNMENT);
buttonContinue.setAlignmentX(Box.CENTER_ALIGNMENT);
buttonOptions.setAlignmentX(Box.CENTER_ALIGNMENT);
buttonExit.setAlignmentX(Box.CENTER_ALIGNMENT);
//Setting button sizes
buttonPlay.setMaximumSize(menuButtonSize);
buttonContinue.setMaximumSize(menuButtonSize);
buttonOptions.setMaximumSize(menuButtonSize);
buttonExit.setMaximumSize(menuButtonSize);
}
}
Literally if i get ride of the titleImage = and add(new JLabel) lines everything goes back to normal
My guess is that you've just got the path wrong -- a common mistake. In that case, you should be getting an exception like:
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at jonathanrmiproject.MyProject.main(JonathanRmiProject.java:24)
This simple example works for me:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.naming.NamingException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyProject {
public static void main(String[] args) throws NamingException, IOException {
BufferedImage img = ImageIO.read(new File("myimage.jpg"));
JLabel label = new JLabel(new ImageIcon(img));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(200, 200);
f.setVisible(true);
}
}
I am using Netbeans IDE, and I have saved the image file to "C:\Users\David.Sharpe\MyProject\myimage.jpg".
I should add that if this is not the case, and you do have the correct path, then you need to post a more detailed question so that someone can help you. Include the code to reproduce the problem.
UPDATE: Wow literally the reason it wasn't showing up was because i called setVisble(true) too early. I can't believe it was something that simple. I guess that explains why one time everything would show up and it would be fine but then every time after nothing showed up.
So in my constructor I had
public class MouseClickGame extends JFrame {
public MouseClickGame() {
super("WELCOME :D");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(middleOfScreen);
setVisible(true);
add(new MouseClickPanel());
}
public static void main(String[] args) {
//Calling constructor
MouseClickGame mainClickGame = new MouseClickGame();
}
}
when all I had to do was put the setVisble() after the add(newMouseClickPanel())
Thank you DavidS for your suggestions :D

IndexOutOfBound when trying Sphinx4

I have created all the models required by Sphinx4 (language model, dictionary and acoustic model). I created a Maven project in Eclipse and all the libraries downloaded, but when I run the program as shown in the official website (http://cmusphinx.sourceforge.net/wiki/tutorialsphinx4), an IndexOutOfBounds Exception is thrown.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 768, Size: 768
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at edu.cmu.sphinx.linguist.acoustic.tiedstate.Pool.get(Pool.java:55)
at edu.cmu.sphinx.linguist.acoustic.tiedstate.Sphinx3Loader.createSenonePool(Sphinx3Loader.java:403)
at edu.cmu.sphinx.linguist.acoustic.tiedstate.Sphinx3Loader.loadModelFiles(Sphinx3Loader.java:341)
at edu.cmu.sphinx.linguist.acoustic.tiedstate.Sphinx3Loader.load(Sphinx3Loader.java:278)
at edu.cmu.sphinx.frontend.AutoCepstrum.newProperties(AutoCepstrum.java:118)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:165)
at edu.cmu.sphinx.util.props.PropertySheet.getComponentList(PropertySheet.java:422)
at edu.cmu.sphinx.frontend.FrontEnd.newProperties(FrontEnd.java:160)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:290)
at edu.cmu.sphinx.decoder.scorer.SimpleAcousticScorer.newProperties(SimpleAcousticScorer.java:46)
at edu.cmu.sphinx.decoder.scorer.ThreadedAcousticScorer.newProperties(ThreadedAcousticScorer.java:130)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:290)
at edu.cmu.sphinx.decoder.search.WordPruningBreadthFirstSearchManager.newProperties(WordPruningBreadthFirstSearchManager.java:201)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:290)
at edu.cmu.sphinx.decoder.AbstractDecoder.newProperties(AbstractDecoder.java:70)
at edu.cmu.sphinx.decoder.Decoder.newProperties(Decoder.java:37)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:290)
at edu.cmu.sphinx.recognizer.Recognizer.newProperties(Recognizer.java:89)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:165)
at edu.cmu.sphinx.api.Context.<init>(Context.java:73)
at edu.cmu.sphinx.api.Context.<init>(Context.java:44)
at edu.cmu.sphinx.api.AbstractSpeechRecognizer.<init>(AbstractSpeechRecognizer.java:37)
at edu.cmu.sphinx.api.LiveSpeechRecognizer.<init>(LiveSpeechRecognizer.java:33)
at Main.main(Main.java:26)
The source code which I am running is as follows:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;
public class Main {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.setAcousticModelPath("Alphabets/acoustic");
configuration.setDictionaryPath("Alphabets/alphabets.dic");
configuration.setLanguageModelPath("Alphabets/alphabets.lm.dmp");
LiveSpeechRecognizer recognizer = null;
try {
recognizer = new LiveSpeechRecognizer(configuration);
} catch (IOException e) {
e.printStackTrace();
}
recognizer.startRecognition(true);
SpeechResult result = recognizer.getResult();
recognizer.stopRecognition();
System.out.println(result.getHypothesis());
result.getLattice().dumpDot("lattice.dot", "lattice");
}
}
I highly appreciate the assistance.
You are trying to use sphinx4 with semi-continuous model. You need to train continuous model to use with sphinx4, see for details
http://cmusphinx.sourceforge.net/wiki/tutorialam
You need to set
$CFG_HMM_TYPE = '.cont.'; # Sphinx 4, Pocketsphinx

Images in runnable jar are not working - NullPointer is thrown

I've seen this question all over this website. And I've read almost every response. I feel like I'm doing exactly what is required, but I just can't get it to work! I'm trying to package some images into a Runnable Jar so that my program is self-contained. When I run the code in Eclipse, it works as intended. But when I use the executable Jar, the program will not launch. It gives me a NullPointerException on the line where I create the image. The files are in a folder called Resources in the source folder of the project. Here is the code. It is incomplete because this is just a test program that I've been trying to get working.
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class testgui extends JFrame{
private static JLabel label = new JLabel();
private static testgui gui = new testgui();
private static ArrayList<ImageIcon> sprites;
public static void main(String[] args) {
// TODO Auto-generated method stub
sprites = getImages();
BufferedImage backgroundImage;
try {
backgroundImage = ImageIO.read(new testgui().getClass().getClassLoader().getResource("resources/runescapemap.png"));
gui.setContentPane(gui.new ImagePanel(backgroundImage));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gui.setLayout(new GridLayout(1,2));
label.setIcon(sprites.get(0));
gui.add(label);
gui.setSize(1000,900);
gui.setVisible(true);
}
private static ArrayList<ImageIcon> getImages(){
ImageIcon autoTalkerLogo = new ImageIcon(new testgui().getClass().getClassLoader().getResource("Resources/autotalker-logo.png"));
ImageIcon meterNormal = new ImageIcon(new testgui().getClass().getClassLoader().getResource("Resources/meter.png"));
ImageIcon meterSafe = new ImageIcon(new testgui().getClass().getClassLoader().getResource("Resources/meter-safe.png"));
ImageIcon meterNotSafe = new ImageIcon(new testgui().getClass().getClassLoader().getResource("Resources/meter-notsafe.png"));
ArrayList<ImageIcon> sprites = new ArrayList<ImageIcon>();
sprites.add(autoTalkerLogo);
sprites.add(meterNormal);
sprites.add(meterSafe);
sprites.add(meterNotSafe);
return sprites;
}
class ImagePanel extends JComponent {
private Image backgroundImage;
public ImagePanel(Image image) {
this.backgroundImage = image;
}
#Override
protected void paintComponent(Graphics g) {
g.drawImage(backgroundImage, 0, 0, null);
}
}
}
If the folder is genuinely called Resources rather than resources, that could be the problem. While the Windows file system is case-insensitive, jar files aren't.
Try
...getResource("Resources/runescapemap.png")
I note that your later calls to getResource do use Resources rather than resources.
Of course, it could be the other way round - maybe your folder is actually resources, and it's the first call that's okay and the other four should use resources. Either way, it's unlikely that both are correct...

convert MHT files to images

Are there any libraries or APIs available to convert MHT files to images? Can we use Universal Document Converter software to do this? Appreciate any thoughts.
If you really want to do this programatically,
MHT
Archived Web Page. When you save a Web
page as a Web archive in Internet
Explorer, the Web page saves this
information in Multipurpose Internet
Mail Extension HTML (MHTML) format
with a .MHT file extension. All
relative links in the Web page are
remapped and the embedded content is
included in the .MHT file.
you can use the JEditorPane to convert this into an Image
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class Test {
private static volatile boolean loaded;
public static void main(String[] args) throws IOException {
loaded = false;
URL url = new URL("http://www.google.com");
JEditorPane editorPane = new JEditorPane();
editorPane.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("page")) {
loaded = true;
}
}
});
editorPane.setPage(url);
while (!loaded) {
Thread.yield();
}
File file = new File("out.png");
componentToImage(editorPane, file);
}
public static void componentToImage(Component comp, File file) throws IOException {
Dimension prefSize = comp.getPreferredSize();
System.out.println("prefSize = " + prefSize);
BufferedImage img = new BufferedImage(prefSize.width, comp.getPreferredSize().height,
BufferedImage.TYPE_INT_ARGB);
Graphics graphics = img.getGraphics();
comp.setSize(prefSize);
comp.paint(graphics);
ImageIO.write(img, "png", file);
}
}

Resources