I have a jpeg image in the form of a byte array. How can I get the byte array in a form where I can strip the comments node of the metadata?
byte[] myimagedata = ...
ImageWriter writer = ImageIO.getImageWritersBySuffix("jpeg").next();
ImageReader reader = ImageIO.getImageReader(writer);
//Looking for file here but have byte array
reader.setInput(new FileImageInputStream(new File(Byte array cant go here)));
IIOMetadata imageMetadata = reader.getImageMetadata(0);
Element tree = (Element) imageMetadata.getAsTree("javax_imageio_jpeg_image_1.0");
NodeList comNL = tree.getElementsByTagName("com");
IIOMetadataNode comNode;
if (comNL.getLength() == 0) {
comNode = new IIOMetadataNode("com");
Node markerSequenceNode = tree.getElementsByTagName("markerSequence").item(0);
markerSequenceNode.insertBefore(comNode,markerSequenceNode.getFirstChild());
} else {
comNode = (IIOMetadataNode) comNL.item(0);
}
You seem you be (just) asking how to create an ImageInputStream that reads from a byte array. From reading the javadocs, I think this should work:
new MemoryCacheImageInputStream(new ByteArrayInputStream(myimagedata))
The FileImageInputStream class doesn't have a constructor that allows you to read from anything but a file in the file system.
The FileCacheImageInputStream would also be an option, but it involves providing a directory in the file system for temporary caching ... and that seems undesirable in this context.
Related
I'm working now for e-book reader written in Java. Primary file type is fb2 which is XML-based type.
Images inside these books stored inside <binary> tags as a long text line (at least it looks like text in text editors).
How can I transform this text in actual pictures in Java? For working with XML I'm using JDOM2 library.
What I've tried does not produce valid pictures (jpeg files):
private void saveCover(Object book) {
// Necessary cast to process with book
Document doc = (Document) book;
// Document root and namespace
Element root = doc.getRootElement();
Namespace ns = root.getNamespace();
Element binaryEl = root.getChild("binary", ns);
String binaryText = binaryEl.getText();
File cover = new File(tempFolderPath + "cover.jpeg");
try (
FileOutputStream fileOut = new FileOutputStream(cover);
BufferedOutputStream bufferOut = new BufferedOutputStream(
fileOut);) {
bufferOut.write(binaryText.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
The image content is specified as being base64 encoded (see: http://wiki.mobileread.com/wiki/FB2#Binary ).
As a consequence, you have to take the text from the binary element and decode it in to binary data (in Java 8 use: java.util.base64 and this method: http://docs.oracle.com/javase/8/docs/api/java/util/Base64.html#getDecoder-- )
If you take the binaryText value from your code, and feed it in to the decoder's decode() method you should get the right byte[] value for the image.
I had embedded a byte array into a pdf file (Java).
Now I am trying to extract that same array.
The array was embedded as a "MOVIE" file.
I couldn't find any clue on how to do that...
Any ideas?
Thanks!
EDIT
I used this code to embed the byte array:
public static void pack(byte[] file) throws IOException, DocumentException{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
writer.addDeveloperExtension(PdfDeveloperExtension.ADOBE_1_7_EXTENSIONLEVEL3);
document.open();
RichMediaAnnotation richMedia = new RichMediaAnnotation(writer, new Rectangle(0,0,0,0));
PdfFileSpecification fs
= PdfFileSpecification.fileEmbedded(writer, null, "test.avi", file);
PdfIndirectReference asset = richMedia.addAsset("test.avi", fs);
RichMediaConfiguration configuration = new RichMediaConfiguration(PdfName.MOVIE);
RichMediaInstance instance = new RichMediaInstance(PdfName.MOVIE);
RichMediaParams flashVars = new RichMediaParams();
instance.setAsset(asset);
configuration.addInstance(instance);
RichMediaActivation activation = new RichMediaActivation();
richMedia.setActivation(activation);
PdfAnnotation richMediaAnnotation = richMedia.createAnnotation();
richMediaAnnotation.setFlags(PdfAnnotation.FLAGS_PRINT);
writer.addAnnotation(richMediaAnnotation);
document.close();
I have written a brute force method to extract all streams in a PDF and store them as a file without an extension:
public static final String SRC = "resources/pdfs/image.pdf";
public static final String DEST = "results/parse/stream%s";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ExtractStreams().parse(SRC, DEST);
}
public void parse(String src, String dest) throws IOException {
PdfReader reader = new PdfReader(src);
PdfObject obj;
for (int i = 1; i <= reader.getXrefSize(); i++) {
obj = reader.getPdfObject(i);
if (obj != null && obj.isStream()) {
PRStream stream = (PRStream)obj;
byte[] b;
try {
b = PdfReader.getStreamBytes(stream);
}
catch(UnsupportedPdfException e) {
b = PdfReader.getStreamBytesRaw(stream);
}
FileOutputStream fos = new FileOutputStream(String.format(dest, i));
fos.write(b);
fos.flush();
fos.close();
}
}
}
Note that I get all PDF objects that are streams as a PRStream object. I also use two different methods:
When I use PdfReader.getStreamBytes(stream), iText will look at the filter. For instance: page content streams consists of PDF syntax that is compressed using /FlateDecode. By using PdfReader.getStreamBytes(stream), you will get the uncompressed PDF syntax.
Not all filters are supported in iText. Take for instance /DCTDecode which is the filter used to store JPEGs inside a PDF. Why and how would you "decode" such a stream? You wouldn't, and that's when we use PdfReader.getStreamBytesRaw(stream) which is also the method you need to get your AVI-bytes from your PDF.
This example already gives you the methods you'll certainly need to extract PDF streams. Now it's up to you to find the path to the stream you need. That calls for iText RUPS. With iText RUPS you can look at the internal structure of a PDF file. In your case, you need to find the annotations as is done in this question: All links of existing pdf change the action property to inherit zoom - iText library
You loop over the page dictionaries, then loop over the /Annots array of this dictionary (if it's present), but instead of checking for /Link annotations (which is what was asked in the question I refer to), you have to check for /RichMedia annotations and from there examine the assets until you find the stream that contains the AVI file. RUPS will show you how to dive into the annotation dictionary.
As the title says, I'm trying to use custom quantization tables to compress an image in JPEG format. My problem is the resulting file can't be opened and the error is:
Quantization table 0x00 was not defined
This is how my code looks like:
JPEGImageWriteParam params = new JPEGImageWriteParam(null);
if (mQMatrix != null) {
JPEGHuffmanTable[] huffmanDcTables = {JPEGHuffmanTable.StdDCLuminance, JPEGHuffmanTable.StdDCChrominance};
JPEGHuffmanTable[] huffmanAcTables = {JPEGHuffmanTable.StdACLuminance, JPEGHuffmanTable.StdACChrominance};
dumpMatrices(mQMatrix);
params.setEncodeTables(mQMatrix, huffmanDcTables, huffmanAcTables);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Iterator writers = ImageIO.getImageWritersByFormatName("JPEG");
ImageWriter imageWriter = (ImageWriter) writers.next();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);
imageWriter.write(null, new IIOImage(mSourceImage, null, null), params);
mCompressedImageSize = outputStream.size();
try (FileOutputStream fileOutputStream = new FileOutputStream(mOutFileName)) {
fileOutputStream.write(outputStream.toByteArray());
}
mCompressedImage = ImageIO.read(new ByteArrayInputStream(outputStream.toByteArray()));
My guess is that it has something to do with the metadata, but I had no luck finding a solution.
Thanks,
R.
UPDATE: Using a hex viewer I determined that the quantization table (DQT - 0xFF, 0xDB section) isn't getting written to the output file. I'm assuming I have to force it to be written somehow.
UPDATE 2: So after actually debugging execution, what I found is that if the tables are set in the parameters object, then metadata isn't generated for neither the quantization not the Huffman tables. If the metadata is missing, then the tables aren't being written in the file. The thing is I see no way to customize the contents of the metadata.
Very interesting question, and unfortunately non-trivial... Here's what I've found:
First of all, using JPEGImageWriteParam.setEncodeTables(...) won't do. From the JavaDoc:
Sets the quantization and Huffman tables to use in encoding abbreviated streams.
And further from JPEG Metadata Format Specification and Usage Notes:
This ordering implements the design intention that tables should be included in JPEGImageWriteParams only as a means of specifying tables when no other source is available, and this can occur only when writing to an abbreviated stream without tables using known non-standard tables for compression.
I.e., the param option can only be used for writing "abbreviated streams" (customs JPEGs without tables, assuming the tables will be provided when reading back). Conclusion: The only way we can specify tables to be encoded with the JPEG, is to pass it in the meta data.
From the same document mentioned above, the tables in the metadata will be ignored and replaced unless compression mode is MODE_COPY_FROM_METADATA, so we need to specify that.
See the Image Metadata DTD for documentation on the metadata structure. The important parts are the dqt and dht nodes with sub-nodes, and their "User object"s (not to be confused with normal DOM "userData"). We need to update these nodes, with the new tables we want to use.
Here's the code I came up with:
// Obtain qtables
mQMatrix = ...;
// Read source image
ImageInputStream imageInputStream = ImageIO.createImageInputStream(...);
ImageReader reader = ImageIO.getImageReaders(imageInputStream).next();
reader.setInput(imageInputStream);
mSourceImage = reader.read(0);
IIOMetadata metadata = null;
// We need the imageWriter to create the default JPEG metadata
ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("JPEG").next();
if (mQMatrix != null) {
dumpMatrices(mQMatrix);
// Obtain default image metadata data, in native JPEG format
metadata = imageWriter.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(mSourceImage), null);
IIOMetadataNode nativeMeta = (IIOMetadataNode) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
// Update dqt to values from mQMatrix
NodeList dqtables = nativeMeta.getElementsByTagName("dqtable");
for (int i = 0; i < dqtables.getLength(); i++) {
IIOMetadataNode dqt = (IIOMetadataNode) dqtables.item(i);
int dqtId = Integer.parseInt(dqt.getAttribute("qtableId"));
dqt.setUserObject(mQMatrix[dqtId]);
}
// For some reason, we need dht explicitly defined, when using MODE_COPY_FROM_METADATA...
NodeList dhtables = nativeMeta.getElementsByTagName("dhtable");
// Just use defaults for dht
JPEGHuffmanTable[] huffmanDcTables = {JPEGHuffmanTable.StdDCLuminance, JPEGHuffmanTable.StdDCChrominance};
JPEGHuffmanTable[] huffmanAcTables = {JPEGHuffmanTable.StdACLuminance, JPEGHuffmanTable.StdACChrominance};
// Update dht
for (int i = 0; i < dhtables.getLength(); i++) {
IIOMetadataNode dht = (IIOMetadataNode) dhtables.item(i);
int dhtClass = Integer.parseInt(dht.getAttribute("class")); // 0: DC, 1: AC
int dhtId = Integer.parseInt(dht.getAttribute("htableId"));
dht.setUserObject(dhtClass == 0 ? huffmanDcTables[dhtId] : huffmanAcTables[dhtId]);
}
// Merge updated tree back (important!)
metadata.mergeTree("javax_imageio_jpeg_image_1.0", nativeMeta);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);
// See http://docs.oracle.com/javase/6/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html#tables
JPEGImageWriteParam params = new JPEGImageWriteParam(null);
params.setCompressionMode(metadata == null ? MODE_DEFAULT : MODE_COPY_FROM_METADATA); // Unless MODE_COPY_FROM_METADATA, tables will be created!
imageWriter.write(null, new IIOImage(mSourceImage, null, metadata), params);
imageOutputStream.close();
mCompressedImageSize = outputStream.size();
try (FileOutputStream fileOutputStream = new FileOutputStream(mOutFileName)) {
fileOutputStream.write(outputStream.toByteArray());
}
mCompressedImage = ImageIO.read(new ByteArrayInputStream(outputStream.toByteArray()));
I am creating an application which will read image byte/pixel/data from an .bmp image and store it in an byte/char/int/etc array.
Now, from this array, I want to subtract 10 (in decimal) from the data stored in the 10th index of an array.
I am able to successfully store the image information in the array created. But when I try to write the array information back to .bmp image, the image created is not viewable.
This is the piece of code which I tried to do so.
In this code, I am not subtracting 10 from the 10th index of an array.
public class Test1 {
public static void main(String[] args) throws IOException{
File inputFile = new File("d://test.bmp");
FileReader inputStream = new FileReader("d://test.bmp");
FileOutputStream outputStream = new FileOutputStream("d://test1.bmp");
/*
* Create byte array large enough to hold the content of the file.
* Use File.length to determine size of the file in bytes.
*/
char fileContent[] = new char[(int)inputFile.length()];
for(int i = 0; i < (int)inputFile.length(); i++){
fileContent[i] = (char) inputStream.read();
}
for(int i = 0; i < (int)inputFile.length(); i++){
outputStream.write(fileContent[i]);
}
}
}
Instead of char[], use byte[]
Here's a modified version if your code which works:
public class Test {
public static void main(String[] args) throws IOException {
File inputFile = new File("someinputfile.bmp");
FileOutputStream outputStream = new FileOutputStream("outputfile.bmp");
/*
* Create byte array large enough to hold the content of the file.
* Use File.length to determine size of the file in bytes.
*/
byte fileContent[] = new byte[(int)inputFile.length()];
new FileInputStream(inputFile).read(fileContent);
for(int i = 0; i < (int)inputFile.length(); i++){
outputStream.write(fileContent[i]);
}
outputStream.close();
}
}
To make your existing code work you should replace the FileReader with a FileInputStream. According to the FileReader javadoc:
FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.
Modifying your sample as below
public static void main(String[] args) throws IOException
{
File inputFile = new File("d://test.bmp");
FileInputStream inputStream = new FileInputStream("d://test.bmp");
FileOutputStream outputStream = new FileOutputStream("d://test1.bmp");
/*
* Create byte array large enough to hold the content of the file.
* Use File.length to determine size of the file in bytes.
*/
byte fileContent[] = new byte[(int)inputFile.length()];
for(int i = 0; i < (int)inputFile.length(); i++){
fileContent[i] = (byte) inputStream.read();
}
inputStream.close();
for(int i = 0; i < (int)inputFile.length(); i++){
outputStream.write(fileContent[i]);
}
outputStream.flush();
outputStream.close();
}
This work for me to create a copy of the original image.
Though as mentioned in the comments above this is probably not the correct approach for what you are trying to achieve.
Other have pointed you at errors in your code (using char instead of byte mostly), however, even if you fix that, you probably will end up with a non-loadable image if you change the value of the 10th byte in the file.
This is because, a .bmp image file starts with an header containing information about the file (color depth, dimensions, ... see BMP file format) before any actual image data. Specifically, the 10th byte is part of a 4 byte integer storing the offset of the actual image data (pixel array). So subtracting 10 from this value will probably make the offset pointing at the wrong point in the file, and your image loader doing bound checking will probably consider this invalid.
What you really want to do is load the image as an image and manipulate the pixel values directly. Something like that:
BufferedImage originalImage = ImageIO.read(new File("d://test.bmp"));
int rgb = originalImage.getRGB(10, 0);
originalImage.setRGB(rgb >= 10 ? rgb - 10 : 0);
ImageIO.write(originalImage, "bmp", new File("d://test1.bmp"));
I am currently using javax.imageio.ImageIO to write a PNG file. I would like to include a tEXt chunk (and indeed any of the chunks listed here), but can see no means of doing so.
By the looks of com.sun.imageio.plugins.png.PNGMetadata it should be possible.
I should be most grateful for any clues or answers.
M.
The solution I struck upon after some decompilation, goes as follows ...
RenderedImage image = getMyImage();
Iterator<ImageWriter> iterator = ImageIO.getImageWritersBySuffix( "png" );
if(!iterator.hasNext()) throw new Error( "No image writer for PNG" );
ImageWriter imagewriter = iterator.next();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
imagewriter.setOutput( ImageIO.createImageOutputStream( bytes ) );
// Create & populate metadata
PNGMetadata metadata = new PNGMetadata();
// see http://www.w3.org/TR/PNG-Chunks.html#C.tEXt for standardized keywords
metadata.tEXt_keyword.add( "Title" );
metadata.tEXt_text.add( "Mandelbrot" );
metadata.tEXt_keyword.add( "Comment" );
metadata.tEXt_text.add( "..." );
metadata.tEXt_keyword.add( "MandelbrotCoords" ); // custom keyword
metadata.tEXt_text.add( fractal.getCoords().toString() );
// Render the PNG to memory
IIOImage iioImage = new IIOImage( image, null, null );
iioImage.setMetadata( metadata ); // Attach the metadata
imagewriter.write( null, iioImage, null );
I realise this question is long since answered, but if you want to do it without dipping into the "com.sun" hierarchy, here's an quick and very ugly example as I couldn't find this documented anywhere else.
BufferedImage img = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
// Create a DOM Document describing the metadata;
// I've gone the quick and dirty route. The description for PNG is at
// [http://download.oracle.com/javase/1.4.2/docs/api/javax/imageio/metadata/doc-files/png_metadata.html][1]
Calendar c = Calendar.getInstance();
String xml = "<?xml version='1.0'?><javax_imageio_png_1.0><tIME year='"+c.get(c.YEAR)+"' month='"+(c.get(c.MONTH)+1)+"' day='"+c.get(c.DAY_OF_MONTH)+"' hour='"+c.get(c.HOUR_OF_DAY)+"' minute='"+c.get(c.MINUTE)+"' second='"+c.get(c.SECOND)+"'/><pHYs pixelsPerUnitXAxis='"+11811+"' pixelsPerUnitYAxis='"+11811+"' unitSpecifier='meter'/></javax_imageio_png_1.0>";
DOMResult domresult = new DOMResult();
TransformerFactory.newInstance().newTransformer().transform(new StreamSource(new StringReader(xml)), domresult);
Document document = dom.getResult();
// Apply the metadata to the image
ImageWriter writer = (ImageWriter)ImageIO.getImageWritersBySuffix("png").next();
IIOMetadata meta = writer.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
meta.setFromTree(meta.getMetadataFormatNames()[0], document.getFirstChild());
FileOutputStream out = new FileOutputStream("out.png");
writer.setOutput(ImageIO.createImageOutputStream(out));
writer.write(new IIOImage(img, null, meta));
out.close();
Using Java 1.6, I edited Mike's code to
Node document = domresult.getNode();
instead of his line
Document document = dom.getResult();
Moreover, I'd suggest to add a line
writer.dispose()
after the job has been done, so that any resources held by the writer are released.
We do this in the JGraphX project. Download the source code and have a look in the com.mxgraph.util.png package, there you'll find three classes for encoding that we copied from the Apache Batik sources. An example of usage is in com.mxgraph.examples.swing.editor.EditorActions in the saveXmlPng method. Slightly edited the code looks like:
mxPngEncodeParam param = mxPngEncodeParam
.getDefaultEncodeParam(image);
param.setCompressedText(new String[] { "mxGraphModel", xml });
// Saves as a PNG file
FileOutputStream outputStream = new FileOutputStream(new File(
filename));
try
{
mxPngImageEncoder encoder = new mxPngImageEncoder(outputStream,
param);
if (image != null)
{
encoder.encode(image);
}
}
finally
{
outputStream.close();
}
Where image is the BufferedImage that will form the .PNG and xml is the string we wish to place in the iTxt section. "mxGraphModel" is the key for that xml string (the section comprises some number of key/value pairs), obviously you replace that with your key.
Also under com.mxgraph.util.png we've written a really simple class that extracts the iTxt without processing the whole image. You could apply the same idea for the tEXt chunk using mxPngEncodeParam.setText instead of setCompressedText(), but the compressed text section does allow for considerable larger text sections.
Try the Sixlegs Java PNG library (http://sixlegs.com/software/png/).
It claims to have support for all chunk types and does private chunk handling.
Old question, but... PNGJ gives full control for reading and writing PNG chunks