Taking a bytearray and creating and writing a JPEG - java

I have a program that
Accepts an encoded base64 string
Converts it to a byte array.
It does this fine.
The final step involves writing this byte array to a file. For example C:\example.jpg.
I know simply writing the bytes won't work so was not sure what I would need to do to take the byte array and create a jpg with the picture that is coming in.
I have to actually send the picture as an attachment in email, but for testing purposes wanted to see if I can see a file saved and when I open up the jpg opens up. Thanks.

I'm guessing you're in need of something like this?
BufferedImage image = ImageIO.read( new ByteArrayInputStream( byteArray ) );
ImageIO.write(image, "BMP", new File("filename.bmp"));
Check out this question and answer, that's where I acquired it from:
How to create a BMP file from raw byte[] in Java

Related

Store information in a PNG file without distorting the image?

I want to write informations (former: int/String) into a PNG-file without making the changes visible to the human eye.
In order to achieve this i extract the file in the form of a Byte array:
byte[] imageAsBytes = fileInputStream.readAllBytes();
In the next step i jump the header (8 Bytes, already tried up to 300Bytes). I already have my information processed and can extract it bitwise.
I replace the last Bit from the picture`s Bytes with the Bitwise extracted information.
This works well with the .bmp file format but applying this technique to PNG-files distorts the image.
My guess is that the PNG-file contains additional information after the header.
So is there any information stored after the header and if so what marks this "information-part" as one?
In order to read and decode a .png file we can use BufferedImage as it follows:
File imageFile = new File("myimage.png");
BufferedImage image = ImageIO.read(imageFile);
No we can manipulate its pixels using getRGB and setRGB methods:
int pixel = image.getRGB(x, y);
// do some byte manipulation on the pixel
// ....
image.setRGB(x, y, pixel);
After this the BufferedImage should be piped to an output stream to be able to save the new image.
I think the best way will be not look at file bytes, but open it with some png decoder then change pixels at image itslef then save it again using png. When You will play with file bytes itself for sure You will broke the format of png, You didnt in bmp becasue bmp its jsut pixel by pixel saved drectly to file wihout any packaging or encoding

Encode opencv::Mat (grayscale) image to .jpg format and generate a byte[ ]

I need some guidance to encode an OpenCV Mat image to .jpg format and create a byte array from that. This byte array will be used to recreate the original image using another application which does not have OpenCV library installed.
Basically, I need to create a byte array of a Mat image from a c++ appllication(OpenCV installed) and recreate it using a java app(no OpenCV).
so far I have tried,
std::vector<uchar> buffer;
cv::imencode(".jpg", returnImage, buffer);
uchar * byteArray = buffer.data();
in here the buffer has large amount of values but buffer.data() contains only first few values from buffer and then "incomplete sequence\340".
I would like to convert Mat image to a byte[] without resaving it to the disk and the created byte array should be able to use in Java to recreate the original .jpg image.
any suggestions to achieve this?

Java library to convert bmp to jpeg and specify compression [duplicate]

How do you convert bmp to jpg in Java? I know how to use the ImageIO way but is there a much faster or better way of doing it?
This is the ImageIO way of doing that I found on the web.
`//Create file for the source
File input = new File("c:/temp/image.bmp");
//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);`
//Create a file for the output
File output = new File("c:/temp/image.jpg");
//Write the image to the destination as a JPG
ImageIO.write(image, "jpg", output);
If I use this way will I lose quality?
Thanks
Yes you will. Actually regardless of the way to convert a BMP (lossless) to JPG (lossy) you always lose quality. You can limit the damage if you set the JPG quality to 100% (which kind of defeats the purpose in my opinion).
Use this tutorial to fix it.

PNG File format and Image bytes?

I am currently trying to write a program to encode text into a png file only changing the least significant bit for each letter i want to encode in the picture, example
I have a 'A' which is 65 and I use 8 different bytes to encode the letter A. So
01010100<- 10101101<- 11011010<- 10101010<- each of these I change the last bit and the put
10110110<- 01010100<- 01010100<- 01010101<- them together so 65 is 01000001 each number by
the arrow is changed according to the 65.
If I should approach this a different way suggestions would be awesome :). This is just
a fun little project I wanted to do. But anyways back to my question.
When I read in a image that is only 4 pixels big I get like 680 bytes which is crazy, or at least I think it is, maybe im wrong? 4 pixels with ARGB at 8 bits each should be 16 bytes with a few bytes im sure to tell the operating system that it is a png and how to handle it. So i was expecting maybe like 30 bytes. Maybe less. Am I looking at this the wrong way? When png images are compressed do they become bigger if it is a small picture? And also, when I was saving it back to the Hard drive I always got a larger file. The original picture was 8,554 kb and then it turned into like 16kb when I saved it back. Here is the code for getting the image bytes and for saving the image. Maybe I am doing something wrong or I am just not understanding it correctly.
These are the ways I get the image (I tried 2 different things)
// BufferedImage img = ImageIO.read(new File("image.png"));
BufferedImage img= robot.createScreenCapture(new Rectangle(1,2,2,2));
how I saved two different ways again.
try {
InputStream in = new ByteArrayInputStream(imgBytes);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "png", new File(
"image.png"));
//FileOutputStream fos = new FileOutputStream("image.png");
//fos.write(b);
//fos.close();
}catch(Exception e){}
How I got the bytes from the Image, again I tried two different ways, the second way that is commented out actually did give me the 16 bytes like I want but when I saved it the Windows couldnt Open it because it didnt know what it was i guess? Not sure, just said file not supported.
byte[] imageBytes = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos );
baos.flush();
imageBytes = baos.toByteArray();
baos.close();
}catch(IOException e){System.out.println(e.getMessage());}
// imageBytes = ((DataBufferByte) image.getData().getDataBuffer()).getData();
return imageBytes;
Thanks!
A png consists of a lot of image meta data as well as the raw image data. That is what is giving you crazy 680 bytes.
I had pretty much the same problem with my barcode fonts. I just wanted to encode 5 bits of data into the smallest PNG I could get. What I didn't want to do is write a custom program, based on libpng. I tried quite a few editors and the smallest file size I could get was around 170 bytes.
Finally I found Gimp 2.0. There is a feature you can use to export a PNG file without all the metadata. I also changed to 8 bit grayscale. I think I could shave a couple of bytes off by switching to 2 bit grayscale, but Gimp wouldn't do that for me. In the end, I was happy with ~75 bytes per character.

converting bmp to jpg in java

How do you convert bmp to jpg in Java? I know how to use the ImageIO way but is there a much faster or better way of doing it?
This is the ImageIO way of doing that I found on the web.
`//Create file for the source
File input = new File("c:/temp/image.bmp");
//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);`
//Create a file for the output
File output = new File("c:/temp/image.jpg");
//Write the image to the destination as a JPG
ImageIO.write(image, "jpg", output);
If I use this way will I lose quality?
Thanks
Yes you will. Actually regardless of the way to convert a BMP (lossless) to JPG (lossy) you always lose quality. You can limit the damage if you set the JPG quality to 100% (which kind of defeats the purpose in my opinion).
Use this tutorial to fix it.

Categories

Resources