hi , am converting gray image to byte array and v disice versa by using bytearrayoutputstream and inputstream but it didn't displayed image am try by getting width and hieght but tere difference in size any body help me please with this problem
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
hi , am converting gray image to byte array and v disice versa by using bytearrayoutputstream and inputstream but it didn't displayed image am try by getting width and hieght but tere difference in size any body help me please with this problem
How are you testing the code?
I don't understand what conversion is needed.
An image is already an array of bytes.
Post a small complete program that compiles, executes and shows the problem.
If you don't understand my answer, don't ignore it, ask a question.
Hi
i have problem with converting gray image to byte array and byte array to image in java, the size of input image are difference
with the output image ,anybody can help me
Please read the forum announcements - I have merged your seemingly identical threads.
I suggest posting an SSCCE as Norm suggested, unless you wish someone to take a wild guess.
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageProMainClass2 {
public ImageProMainClass2() {
}
public static byte[] imgFileToByteArray(String imgFileName){
int width = 0 ;
int height = 0 ;
byte[] readData = null;
try {
File imgPath = new File(imgFileName);
BufferedImage bufferedImage = ImageIO.read(imgPath);
// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
readData=data.getData();
//return ( readData );
} catch (IOException e) {
e.printStackTrace();
}
return readData;
}
public static BufferedImage byteArrayToBufImg(byte[] array)
{
BufferedImage imgout = new BufferedImage(12, 12, BufferedImage.TYPE_INT_ARGB_PRE);
WritableRaster wr = imgout.getRaster();
int data = 0 ;
int index = 0 ;
for(int i = 0 ; i<12 ;i++){
for(int j = 0 ; j<12 ;j++){
wr.setSample(i, j, 0, array[index]);
index++;
}
}
for(int i = 0 ; i<array.length;i++){
System.out.println("this is array2["+i+"]:"+array[i]);
}
return imgout;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String filename = "strawberry.png" ;
byte[] imgData = imgFileToByteArray(filename);
BufferedImage img = byteArrayToBufImg(imgData);
try {
ImageIO.write(img, "png", new File("out2.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
--- Update ---
thanks very much for answer my question i convert image to array of byte and then convert the array of byte to image again , but when i convert the array of byte to image the image difference with the original image in size, i take gray scale png image
regards
Can you post some data or output showing what the problem is?
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.
If you don't understand my answer, don't ignore it, ask a question.
import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.awt.image.WritableRaster; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageProMainClass2 { public ImageProMainClass2() { } public static byte[] imgFileToByteArray(String imgFileName){ int width = 0 ; int height = 0 ; byte[] readData = null; try { File imgPath = new File(imgFileName); BufferedImage bufferedImage = ImageIO.read(imgPath); // get DataBufferBytes from Raster WritableRaster raster = bufferedImage .getRaster(); DataBufferByte data = (DataBufferByte) raster.getDataBuffer(); readData=data.getData(); //return ( readData ); } catch (IOException e) { e.printStackTrace(); } return readData; } public static BufferedImage byteArrayToBufImg(byte[] array) { BufferedImage imgout = new BufferedImage(12, 12, BufferedImage.TYPE_INT_ARGB_PRE); WritableRaster wr = imgout.getRaster(); int data = 0 ; int index = 0 ; for(int i = 0 ; i<12 ;i++){ for(int j = 0 ; j<12 ;j++){ wr.setSample(i, j, 0, array[index]); index++; } } for(int i = 0 ; i<array.length;i++){ System.out.println("this is array2["+i+"]:"+array[i]); } return imgout; } public static void main(String[] args) { // TODO Auto-generated method stub String filename = "strawberry.png" ; byte[] imgData = imgFileToByteArray(filename); BufferedImage img = byteArrayToBufImg(imgData); try { ImageIO.write(img, "png", new File("out2.png")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
i used small logo by size 12*12 the input logo size 28 kb the out is logo 128 bytes am try to do the same code but depending on size and by using input and output stream but i still have size problem issue
regard
The posted code has lost all its formatting.
Why are you using Raster? Could you get the bytes directly from the BufferedImage and restore them directly to a BufferedImage.
If you don't understand my answer, don't ignore it, ask a question.
i get the byte from buffered image and restore them from buffered image but i still have the same problem ,second i test it by image correlation measure to test their similarity the correlation zero , my head go to explode with it
Please post the new code.
If you don't understand my answer, don't ignore it, ask a question.
my code is to long i embed image bit to audio but when restore the image my byte restored correctly but i have problem with reconstruct the image
i get the the error reading png file so i skip 8 byte befor embedding and call the original image in extract just to get the header pa
rt from it
If the image file is not written correctly, I think you would get some kind of error.
Without code that executes, it is very hard to find the problem.
Don't post a large program. Make a SSCCE.
If you don't understand my answer, don't ignore it, ask a question.
File file=new File(imgName); byte[] originalImage=null; try{ // here i read the original image to get the header BufferedImage Image = ImageIO.read(file); ByteArrayOutputStream aos = new ByteArrayOutputStream(); ImageIO.write(Image, "gif", aos); //System.out.println("this is bytearray:"+baos); aos.flush(); originalImage = aos.toByteArray(); // here is the returned bit to reconstruct the image BitSet result=returnphase(); / byte[] array=result.toByteArray(); // skip 14 if it is gif type for (int i=14; i<array.length;i++) {// put the returned in original originalImage[i]=array[i]; } for (int i=0; i<originalImage.length;i++) { // here i concatenate with the header System.out.println("this is origin["+i+"]:"+originalImage[i]); } ByteArrayInputStream in = new ByteArrayInputStream(originalImage); final BufferedImage bufferedImage = ImageIO.read(in); // here i displayed the image in fff file File imagefile =new File("fff.gif"); ImageIO.write(bufferedImage, "gif",imagefile ); }catch (Exception ex) { ex.printStackTrace();
Please post a complete program that will compile, execute and show the problem.
If you don't understand my answer, don't ignore it, ask a question.
i told you my code is too long it i 20 class but i get the errorerror reading png header file ) or any type of image please if you can help me
Please stop posting the same thing in new threads, and please do not hijack threads that are over 4 years old. I have merged the threads. Please keep the discussion of your problem in this thread. If you continue to ignore moderator warnings, you leave us no alternative than to take further action.