Out-of-the-box ArrayIndexOutOfBoundsException when attempting to use Apache-Commons Sanselan to load a TIFF that was compressed with PackBits compression.
Code:
import org.apache.sanselan.*;
public class TIFFHandler {
public static Image loadTIFF(String fileName) throws ImageReadException, IOException {
File imageFile = new File(fileName);
BufferedImage bi = Sanselan.getBufferedImage(imageFile);
return bi;
}
public static void main(String[] args) throws IOException, ImageReadException {
String TIFFFILE = "test_image.tif";
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
BufferedImage bi = (BufferedImage) loadTIFF(TIFFFILE);
ImageIcon ii = new ImageIcon(bi);
JLabel lbl = new JLabel(ii);
panel.add(lbl);
frame.setVisible(true);
}
}
Stack trace:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 426
at org.apache.sanselan.common.PackBits.decompress(PackBits.java:55)
at org.apache.sanselan.formats.tiff.datareaders.DataReader.decompress(DataReader.java:127)
at org.apache.sanselan.formats.tiff.datareaders.DataReaderStrips.readImageData(DataReaderStrips.java:96)
at org.apache.sanselan.formats.tiff.TiffImageParser.getBufferedImage(TiffImageParser.java:505)
at org.apache.sanselan.formats.tiff.TiffDirectory.getTiffImage(TiffDirectory.java:163)
at org.apache.sanselan.formats.tiff.TiffImageParser.getBufferedImage(TiffImageParser.java:441)
at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1264)
at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1255)
at TIFFHandler.loadTIFF(FieldSheetHandler.java:42)
at TIFFHandler.main(FieldSheetHandler.java:90)
I've attempted an analysis of the problem, but I'm pretty lost...any directions would be really helpful. TIFF images are a pain in the a**.
You can try the updated version of Commons Imaging from the Apache snapshot repository. The Javadoc is not online yet, you'll have to build it by checking out the code from SVN and running mvn javadoc:javadoc.
If you find more issues or want to suggest an improvement you can file them in JIRA. Also the developers will be happy to help you if you have questions regarding the usage of the API. They await you on the mailing list.
Related
this is the code where I am converting MathMl to png
public static void main(String[] args) throws IOException, FontFormatException {
Converter converter = Converter.getInstance();
String math="<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mfenced open=\"[\" close=\"]\"><mtable><mtr><mtd><mn>3</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>6</mn></mtd><mtd><mn>7</mn></mtd></mtr><mtr><mtd><mn>6</mn></mtd><mtd><mn>4</mn></mtd></mtr></mtable></mfenced><mo>=</mo><msubsup><mo>∫</mo><mn>5</mn><mn>4</mn></msubsup></math>";
File inputFile = new File("D:\\mathml.xml");
File outputFile = new File("D:\\image.jpg");
//params to mention the size of image
MutableLayoutContext params = new LayoutContextImpl(
LayoutContextImpl.getDefaultLayoutContext());
params.setParameter(Parameter.MATHSIZE, 50f);
Document doc = StringToDocumentToString.convertStringToDocument(math);
// Parameter parameter= new Pa
converter.convert(doc, outputFile , "image/jpeg", params);
}
I want to use Tahoma.ttf when I make png from MathMl but I can not find any resource how to do that. Please can anyone help me?
i want to upload an image via primefaces:fileUpload and then display it on a div for example with css.
I can already save the image on the server:
public void upload() throws IOException, URISyntaxException {
if (logo != null) {
File fileImage = new File(System.getProperty("jboss.server.data.dir"), "uploads.png");
BufferedImage img = ImageIO.read(new ByteArrayInputStream(logo.getContents()));
if (fileImage.exists()) {
fileImage.delete();
}
ImageIO.write(img, "png", fileImage);
}
}
And then i tried to get the web path to the file but that didn't worked:
public String getImagePath(){
File fileImage = new File(System.getProperty("jboss.server.data.dir"), "uploads.png");
Set<String> set = FacesContext.getCurrentInstance().getExternalContext().getResourcePaths(fileImage.getAbsolutePath());
return set.iterator().next();
}
I need something like this:
/ewarehouse/javax.faces.resource/dynamiccontent.properties.xhtml?ln=primefaces&v=6.2&v=6.2&pfdrid=f52e395e4f38c09a1990e8f9d0c5806d&pfdrt=sc&pfdrid_c=true
Can someone help me or has a other way to do this ?
It worked for me to create a servlet and refered to the file position.
background-image: url('#{'/'}ewarehouse#{'/'}images//dynamic#{'/'}?file=uploads.png')
In css it look a bit weird but this way it worked Thanks for the answer to #JasperdeVries and #Kukeltje
I have coded a program in Eclipse and it works properly when I run in that.
public static void initialize() throws IOException{
JTextField tfQrText;
int size = 250;
File qrFile;
BufferedImage qrBufferedImage;
JFrame gui = new JFrame("qrCode Generator");
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(250, 340);
gui.setLayout(new BorderLayout());
gui.setResizable(false);
File iconFile = new File(VisualQrCodeGenerator.class.getResource("icon.png").getFile());
BufferedImage iconBuffered = ImageIO.read(iconFile);
gui.setIconImage(iconBuffered);
JButton generate = new JButton("Generate qrCode");
gui.add(generate,BorderLayout.SOUTH);
tfQrText = new JTextField();
PromptSupport.setPrompt("Enter Your Text ... ", tfQrText);
gui.add(tfQrText,BorderLayout.NORTH);
qrFile = new File(VisualQrCodeGenerator.class.getResource("qrCodeImage.png").getFile());
qrBufferedImage = ImageIO.read(qrFile);
ImageIcon qrImageIcon = new ImageIcon(qrBufferedImage);
JLabel image = new JLabel();
image.setIcon(qrImageIcon);
image.setHorizontalAlignment(JLabel.CENTER);
gui.add(image,BorderLayout.CENTER);
gui.setVisible(true);
generate.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if(!(tfQrText.getText().equals(""))){
//create() Method make the QRcode Image
create();
}
}
});
The Error occurs on line:
BufferedImage iconBuffered = ImageIO.read(iconFile);
When I make it .jar file, it says:
My Project structure is like this:
+src
+qrCodeGenerator
-VisualQrCodeGenerator
-icon.png
-qrCodeImage.png
The code is running properly and without any error in program and I can work with it. But when I make it .jar file, it errors me as I uploaded it image.
This is normal: you can't access a classpath resource as a File object. This is because it is embedded inside a JAR. It works inside your IDE because resources are typically stored inside a temporary folder (and not inside a JAR).
You need to access it with an InputStream using Class.getResourceAsStream and use ImageIO.read(InputStream) instead.
As such, change your code to:
qrBufferedImage = ImageIO.read(VisualQrCodeGenerator.class.getResourceAsStream("qrCodeImage.png"));
and
iconBuffered = ImageIO.read(VisualQrCodeGenerator.class.getResourceAsStream("icon.png"));
I know what the problem is I just do not know how to fix it. So I have an image that I am trying to render in my program. I use ImageIO to load the image. But it seems to have a problem wit the path I am giving it. I am using NetBeans as my IDE and I dont know if I am saving the image file correctly.
First method:
public void init(){
BufferedImageLoader loader = new BufferedImageLoader();
try{
spriteSheet = loader.loadImage("/sprite_sheet.png");
}catch(IOException e){
e.printStackTrace();
}
SpriteSheet ss = new SpriteSheet(spriteSheet);
player = ss.grabImage(1,1,32,32);
}
the loader BufferedImageLoader class:
public class BufferedImageLoader {
private BufferedImage image;
public BufferedImage loadImage(String path) throws IOException{
image = ImageIO.read(getClass().getResource(path));
return image;
}
}
I have the image saved under a 'res' folder under 'src' folder.
Error:
Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
Thank you.
Why do you need to use getClass().getResource() ?
Most simple usage of ImageIO.read is as follows.
image = ImageIO.read(new File(path));
You may need to add folders to path also.
spriteSheet = loader.loadImage("/src/res/sprite_sheet.png");
Try using an absolute path for your file or if you need a relative check this post (eg assuming you have a res folder under default package did you try "/res/yourfile"
I try to display an image using a JLabel. This is my project navigator:
From SettingsDialog.java I want to display an image using following code:
String path = "/images/sidebar-icon-48.png";
File file = new File(path);
Image image;
try {
image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
header.add(label); // header is a JPanel
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The code throws an exception: Can't read input file!
Is the path of the image is wrong?
Don't read from a file, read from the class path
image = ImageIO.read(getClass().getResource(path));
-or-
image = ImageIO.read(MyClass.class.getResource(path));
When you use a File object, you're telling the program to read from the file system, which will make your path invalid. The path you are using is correct though, when reading from the class path, as you should be doing.
See the wiki on embedded resource. Also see getResource()
UPDATE Test Run
package org.apache.openoffice.sidebar;
import javax.swing.*;
public class SomeClass {
public SomeClass() {
ImageIcon icon = new ImageIcon(
SomeClass.class.getResource("/images/sidebar-icon-48.png"));
JLabel label = new JLabel(icon);
JFrame frame = new JFrame("Test");
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new SomeClass();
}
});
}
}
"/images/sidebar-icon-48.png" is root path. On windows would be c:\images\sidebar-icon-48.png or d:\images\sidebar-icon-48.png depending on current drive (java converts the / to \ - not an issue). Linux images would a child of root /images/sidebar-icon-48.png Need to load relative to class or relative to the jar that had the class (if you do not want to store images inside jar.
In big projects its nice to have images and other resources outside the jar so the jar is smaller and more importantly its easy to change the resources without fiddling with jars/ wars.
Since you seem to be making a add on for open office, you will have to keep everything in jar and so peeskillet answer is right. But make sure your images folder is being packed in the jar. Extract the jar ising the jar command or rename the file to zip and extract.
Or check and fix project settings. How to make a jar in eclipse ... latest one has a wizard that makes an ant script or this SO
try to use this directly :
JLabel label = new JLabel(new ImageIcon(path));
and delete these line :
File file = new File(path);
image = ImageIO.read(file);
if error still exist paste the following error