I am using im4java version 1.4.0 to access ImageMagick functionality from Java. It is working well for processing images to and from files.
The Developers Guide has a section on using Buffered Images instead of writing output to a file, and there is a test (TestCase13) that demonstrate using Buffered Images as output. However, when I run any action with a Buffered Image I receive an org.im4java.core.CommandException stating: no ImageReader for given format.
I have tried a number of different things (including adding the jai_imageio.jar to provide additional formats), but nothing seems to work. A basic test-code that shows the problem, (based on im4java.jar's TestCase13) is:
#Test
public void shouldWorkWithBufferedImageTest() throws InterruptedException, IOException, IM4JavaException {
ProcessStarter.setGlobalSearchPath("C:\\Program Files\\ImageMagick-6.8.9-Q8");
String iImageDir = "C:\\images";
String var1 = "png";
IMOperation imOp = new IMOperation();
imOp.addImage(new String[]{iImageDir + "sample-image=6.png"});
imOp.blur(Double.valueOf(2.0D)).paint(Double.valueOf(10.0D));
imOp.addImage(new String[]{var1 + ":-"});
ConvertCmd convertCmd = new ConvertCmd();
Stream2BufferedImage stream2BufferedImage = new Stream2BufferedImage();
convertCmd.setOutputConsumer(stream2BufferedImage);
convertCmd.run(imOp, new Object[0]);
BufferedImage outImage = stream2BufferedImage.getImage();
ImageIO.write(outImage, "PNG", new File(iImageDir + "tmpfile.png"));
DisplayCmd.show(iImageDir + "tmpfile.png");
}
Running this throws the following error:
org.im4java.core.CommandException: java.lang.IllegalStateException: no ImageReader for given format
at org.im4java.core.ImageCommand.run(ImageCommand.java:219)
at test.groovy.services.ImageManipulation.JavaBufferedImageManipulationTest.shouldWorkBufferedImageTest(JavaBufferedImageManipulationTest.java:31)
Caused by: java.lang.IllegalStateException: no ImageReader for given format
at org.im4java.core.Stream2BufferedImage.consumeOutput(Stream2BufferedImage.java:82)
at org.im4java.process.ProcessStarter.processOutput(ProcessStarter.java:276)
at org.im4java.process.ProcessStarter.access$200(ProcessStarter.java:54)
at org.im4java.process.ProcessStarter$2.call(ProcessStarter.java:433)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.lang.Thread.run(Thread.java:745)
The error occurs on the the convertCmd.run line, however, the problem seems to be with setting the outputConsumer as the stream2BufferedImage. How do I fix this? Is there a known bug with im4java and BufferedImages? Is there a better work-around than exporting to a temp-file and then reading it back in to a BufferedImage? I am aware of JMagick (as an alternative to im4java) but have not found this a good solution for other reasons.
Thanks, in advance, for any assistance or ideas.
see in this example i have the input source as buffered Image and output is also as buffered image . I hope this can help you.
public static void main(String... args) throws Exception {
IMOperation op = new IMOperation();
op.addImage();
op.resize(350)
op.addImage("png:-")
BufferedImage images = ImageIO.read(new File("image.jpg"));
// set up command
ConvertCmd convert = new ConvertCmd();
Stream2BufferedImage s2b = new Stream2BufferedImage();
convert.setOutputConsumer(s2b);
// run command and extract BufferedImage from OutputConsumer
convert.run(op,images);
BufferedImage img = s2b.getImage();
}
Related
I'm working on a .opus music library software which converts audio/video files to .opus files and tags them with metadata automatically.
Previous versions of the program have saved the album art as binary data apparently as revealed by exiftool.
The thing is that when I run the command to output data as binary using the -b option, the entire thing is in binary seemingly. I'm not sure how to get the program to parse it. I was kind of expecting an entry like Picture : 11010010101101101011....
The output looks similar to this though:
How can I parse the picture data so I can reconstruct the image for newer versions of the program? (I'm using Java8_171 on Kubuntu 18.04)
It looks like you're trying to open the raw bytes in a text editor, which will of course give you gobble-dee-gook since those raw bytes do not represent characters that can be displayed by any text editor. I can see from your output from exiftool that you are able to know the length of the image in bytes. Providing you know the beginning byte position in the file, this should make your task relatively easy with a little bit of Java code. If you can get the starting position of the image inside your file, you should be able to do something like:
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class SaveImage {
public static void main(String[] args) throws IOException {
byte[] imageBytes;
try (RandomAccessFile binaryReader =
new RandomAccessFile("your-file.xxx", "r")) {
int dataLength = 0; // Assign this the byte length shown in your
// post instead of zero
int startPos = 0; // I assume you can find this somehow.
// If it's not at the beginning
// change it accordingly.
imageBytes = new byte[dataLength];
binaryReader.read(imageBytes, startPos, dataLength);
}
try (InputStream in = new ByteArrayInputStream(imageBytes)) {
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert,
"jpg", // or whatever file format is appropriate
new File("/path/to/your/file.jpg"));
}
}
}
I have this problem using smartXLS library for Java
I try to export the sheets of a workbook as .png images using the method
workbook.sheetRangeToImage(row1,col1,row2,col2,file)
The code I use is as follows:
private static void takeValuePics() throws Exception {
WorkBook w = new WorkBook();
w.read(XLS_PATH + DIF_FILE_NAME);
int numSheets = w.getNumSheets();
String out;
for(int i=0;i<numSheets;i++) {
w.setSheet(i);
System.out.println(w.getNumber(1,1));
w.setPrintGridLines(true);
out = VAL_PATH + "values_" + w.getSheetName(i) + ".png";
w.sheetRangeToImage(0,0,LOOPS,3,out);
}
The constants are configured correctly and the file is read correctly.
(The println() prints correct values)
The .png files are created but they are completely empty! Just white rectangles.
Does anybody know what's wrong?
Problem solved by the fantastic SmartXLS support team!
I forward their answer:
You need set the print scale,it use the print scale value when exporting range to image
workBook.setPrintScale(100);
That was all!
Hope this helps people in the future.
Now,I am using im4java and imageMagick to deal with pictures.
Recently,it always appears
org.im4java.core.CommandException: return code: 137
At the same time the Tomcat7.0 that is my Application Server will crash.
Of course,my program run in linux.
I do not know how to do.Ask for help
thank you!
My code is
public static void cutImage(int width, String srcPath, String newPath) throws Exception {
IMOperation op = new IMOperation();
op.addImage(srcPath);
op.resize(width, null);
op.addImage(newPath);
ConvertCmd convert = new ConvertCmd();
convert.run(op);
}
When I call this method.The phenomenon will happend sometimes.
I had a simlary issue by generating Thumbnails from Videos.
Like in the Comment from zawhtut the process gets kill from somewhere.
my sfirst solution was to reduce the size of the images (from 1920px to 256px)
I am trying to convert TIF / TIFF images to JPG which works fine but for few TIF images I am getting an IllegalArgumentException: Bad endianness tag (not 0x4949 or 0x4d4d).
Exception :
java.io.IOException: Bad endianness tag (not 0x4949 or 0x4d4d).
at com.sun.media.jai.codecimpl.CodecUtils.toIOException(CodecUtils.java:76)
at com.sun.media.jai.codecimpl.TIFFImageDecoder.getNumPages(TIFFImageDecoder.java:98)
at com.sun.media.jai.codecimpl.TIFFImageDecoder.decodeAsRenderedImage(TIFFImageDecoder.java:103)
at com.sun.media.jai.codec.ImageDecoderImpl.decodeAsRenderedImage(ImageDecoderImpl.java:140)
at com.pkg.jae.utils.GenericImageUtils.convertTiffToJpg(GenericImageUtils.java:35)
at com.pkg.jae.utils.GenericImageUtils.main(GenericImageUtils.java:92)
Caused by: java.lang.IllegalArgumentException: Bad endianness tag (not 0x4949 or 0x4d4d).
at com.sun.media.jai.codec.TIFFDirectory.getNumDirectories(TIFFDirectory.java:595)
at com.sun.media.jai.codecimpl.TIFFImageDecoder.getNumPages(TIFFImageDecoder.java:96)
... 4 more
Code Function :
public static void convertTiffToJpg(String strTiffUrl,String strJpgFileDestinationUrl) throws Exception {
try {
FileSeekableStream obj_FileSeekableStream = new FileSeekableStream(new File(strTiffUrl));
ImageDecoder obj_ImageDecoder = ImageCodec.createImageDecoder(EXT_TIFFX, obj_FileSeekableStream, null);
RenderedImage obj_RenderedImage = obj_ImageDecoder.decodeAsRenderedImage();
JAI.create("filestore", obj_RenderedImage,strJpgFileDestinationUrl, EXT_JEPGX);
obj_RenderedImage = null;
obj_ImageDecoder = null;
obj_FileSeekableStream.close();
} catch (Exception ex) {
throw ex;
}
}
If anyone knows the issue and can help in this.
As stated in a comment by bitbank, this means you're passing a JPEG file to it when it expects to get a TIFF file.
Startlingly, this JAI
RenderedOp renderer = JAI.create("fileload", filename);
BufferedImage bi = renderer.getAsBufferedImage();
does not have the same failure and just works regardless of image "kind." Don't use this particular method (passing in the filename) though, see Is JAI closing file handles too early?
I had this issue and it turned out to be a front-end problem. Yes, I was trying to upload the wrong file type, but I was expecting correct handling and a gracious popup message alert. Instead I was getting the error you described.
In my case, I was using extjs and I had a failure function like this:
failure: function (a) {
...some message alert...
}
instead of:
failure: function (f, a) {
...some message alert...
}
and this was throwing that exception, instead of displaying my message alert.
When I simply create a new image from another like this:
public static void scaleByTwoRight(String src, String dest)
throws IOException {
BufferedImage bsrc = ImageIO.read(new File(src));
int width = bsrc.getWidth()/2;
int height = bsrc.getHeight();
BufferedImage bdest = bsrc.getSubimage(width, 0, width, height);
ImageIO.write(bdest,"PNG",new File(dest));
}
Source file (src) = C:...\Manga\Shonan Juna_ Gumi Tome 11\Shonan Junaï Gumi Tome 11 - 091B.png
Destination file (dest) = C:...\Manga\Shonan Junaï Gumi Tome 11 - 091B_A.png
Example of generated file: https://docs.google.com/file/d/0B1vKCZzB5hxqYzNsUWF5RHA2Wm8/edit?usp=sharing
Problem: The new image has mimetype: application instead of mimetype: image
How I arrive to this conclusion: I'm using a function to test if the file is an image or not:
public static boolean isImage(String src)
throws IOException {
File f = new File(src);
String mimetype= new MimetypesFileTypeMap().getContentType(f);
String type = mimetype.split("/")[0];
if(type.equals("image")){
return true;
}else{
System.out.println("mimetype: "+type);
return false;
}
}
It has not a huge impact if the Mime-type is not correct but I prefer to have that working properly..
Thanks for your help!
Note:
I'm running under Windows 7 / 32b
JVM 1.7 / Eclipse Helios
Your code is working fine in my machine.
I have windows XP,32 bit,
Tried with jpeg image and it is returning the mimetype as image/jpeg only.
Hope you are not trying to execute both the functions simultaneously.
Also the destination file name should contain proper extension like .jpeg or. png etc...