I am getting blank zip even though I am writing images to it .Can some one let me know what I am doing wrong?I am trying to add images to the zip file .Its creating a zip file but with blank entry on it .so why its not writing the file to it?
int i = 0;
List<String> filenames = new ArrayList<String>();
for (Iterator iterator = datas.iterator(); iterator.hasNext();) {
byte[] bs = (byte[]) iterator.next();
BufferedImage image = null;
ByteArrayInputStream bis = new ByteArrayInputStream(bs);
image = ImageIO.read(bis);
bis.close();
// write the image to a file
File outputfile = new File("image" + i + ".jpg");
System.out.println("*************************" + outputfile.getAbsolutePath());
ImageIO.write(image, "jpg", outputfile);
i++;
filenames.add(outputfile.getAbsolutePath());
}
// Create the ZIP file
//create object of FileOutputStream
FileOutputStream fout = new FileOutputStream(zipName);
//create object of ZipOutputStream from FileOutputStream
ZipOutputStream zout = new ZipOutputStream(fout);
byte[] buffer = new byte[2048];
for (int k = 0; k < filenames.size(); k++) {
FileInputStream fis = new FileInputStream(filenames.get(k).toString());
// Add ZIP entry to output stream.
File file = new File(filenames.get(k).toString());
String entryname = file.getName();
zout.putNextEntry(new ZipEntry(entryname));
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
zout.write(buffer, 0, bytesRead);
}
zout.closeEntry();
fis.close();
}
zout.flush();
fout.flush();
zout.close();
fout.close();
I understand that this code finds a photo via path but I don't understand what is in the photo variable
fileName = f.getAbsolutePath();
try {
File image2 = new File(fileName);
FileInputStream fis = new FileInputStream(image2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for(int readNum; (readNum=fis.read(buf))!=-1;) {
bos.write(buf, 0, readNum);
}
photo = bos.toByteArray();
} catch(Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}
The below code is just getting the byte array of that photo , that means the content of photo in byte array format.
fileName = f.getAbsolutePath();
try {
//Getting file object from the filePath
File image2 = new File(fileName);
// create input stream for that image object
FileInputStream fis = new FileInputStream(image2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
// below repeated logic read the fis object till its empty and write into byte array object
for(int readNum; (readNum=fis.read(buf))!=-1;) {
bos.write(buf, 0, readNum);
}
photo = bos.toByteArray();
} catch(Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}
My error when sending an image Client-Server-Client is "Corrupt JPEG data: 4350674 extraneous bytes before marker 0xd9" What I can change? Here is my code :
SERVER
InputStream is = klienciRemoteScreen[1].getInputStream();
OutputStream os = klienciRemoteScreen[0].getOutputStream();
byte[] bytesize = new byte[4];
is.read(bytesize);
int size = ByteBuffer.wrap(bytesize).asIntBuffer().get();
byte [] image= new byte [size];
byte[]bufferimage = new byte [1];
os.write(bytesize);
os.flush();
int count;
while((count = is.read(image)) > 0)
{
os.write(image);
os.flush();
}
os.close();
CLIENT sending image to server
OutputStream os = noguiklient.sockRemoteScreen.getOutputStream();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(dim);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rectangle);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size =
ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
int sizeas = ByteBuffer.wrap(size).asIntBuffer().get();
os.write(size);
os.write(byteArrayOutputStream.toByteArray());
os.close();
CLIENT Receiving from server
JFrame okno = new JFrame();
okno.setSize(1300, 1000);
okno.setTitle(" Remote screen");
okno.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setSize(1300,1000);
panel.setLayout(null);
okno.add(panel);
okno.setVisible(true);
InputStream is = Klient.sockRemoteScreen.getInputStream();
File photo = new File ("photo") ;
FileOutputStream fos = new FileOutputStream(photo);
byte[] buffer = new byte[4];
is.read(buffer);
int size = ByteBuffer.wrap(buffer).asIntBuffer().get();
byte[] image = new byte [size];
int count;
int i =0 ;
while((count = is.read(image)) > 0)
{
fos.write(image);
fos.flush();
}
ImageIcon screen = new ImageIcon("photo");
Image imagefinal = screen.getImage();
imagefinal =
imagefinal.getScaledInstance(okno.getWidth(),okno.getHeight(), Image.SCALE_SMOOTH);
BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics graphics = panel.getGraphics();
while(true)
{
graphics.drawImage(imagefinal, 0, 0, panel.getWidth(), panel.getHeight(), panel);
}
Example of this bad Image
When I try to send with ImageIO everything is fine but I want to try with bytes and there is a problem.
Thank you :)
The code right down to this saving photos from a HTML page is working.
String
nameimage,
srcfoto;
nameimage = vs.getValor("NAMEIMAGE");
srcfoto = vs.getValor("img").toString();
URL url = new URL(srcfoto);
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1 != (n = in.read(buf))) {
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream("C:\\img\\ " + nameimage + ".png");
fos.write(response);
fos.close();
vs.setReturnOK("Image save!");
return vs.getReturnOK();
My problem is that I need to download the following canvas src..
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAABkCAYAAABwx8J9AAAeKElEQVR4Xu2de+h2S1XHNfqvLLCLlZW/MjAyTyiR1Sk5kHTT7hfs/hJRpySlLEvKhILSPJ0g0mNF9RbdMel+kcwjncIEuxsJnfqJ3ROR6vSHRLU+x2fZas7sPWv2nv1c9vt9YPE+7++ZmT3zXWvmO7NmzeyHP0wfISAEhIAQEAJC4OIRePjFt0ANEAJCQAgIASEgBB4mQpcRCAEhIASEgBDYAQIi9B0oUU0QAkJACAgBISBClw0IASEgBISAENgBAiL0HShRTRACQkAICAEhIEKXDQgBISAEhIAQ2AECIvQdKFFNEAJCQAgIASEgQpcNCAEhIASEgBDYAQIi9B0oUU0QAkJACAgBISBClw0IASEgBISAENgBAiL0HShRTRACQkAICAEhIEKXDQgBISAEhIAQ2AECIvQdKFFNEAJCQAgIASEgQpcNCAEhIASEgBDYAQIi9B0oUU0QAkJACAg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BkCIjQTwa9HiwEhIAQEAJCYBwCIvRxWKokISAEhIAQEAInQ0CEfjLo9WAhIASEgBAQAuMQ+F9sgNYKIgd0wQAAAABJRU5ErkJggg==
Only by accepting this src, example /image.png and don't data:image
In debug...
I was reading a bit on this question but did not understand, and do not know if this is the problem Convert Data URI to File then append to FormData
any help is welcome...
I am sending a bufferedImage over a socket and I am using the example found in this post:
Sender
BufferedImage image = ....;
ImageIO.write(image, "PNG", socket.getOutputStream());
Receiver
BufferedImage image = ImageIO.read(socket.getInputStream());
It works - IF, and ONLY IF, I close the sender's outputStream after this line:
ImageIO.write(image, "PNG", socket.getOutputStream());
Is there anything I can do apart from closing the outputStream?
Also, is there anything else I can do to avoid using ImageIO altogether? It seems to take ages to do anything.
Also note that reading or writing to the hard disk in anyway should be avoided at all costs due to performance issues. I need to make this transfer as fast as possible, (I'm experimenting and trying to create a client similar to VNC and saving each screenshot to the hard disk would greatly slow down everything)..
#Jon Skeet
Edit 3:
Sender: (Note that I am sending a JPG image not a PNG).
int filesize;
OutputStream out = c.getClientSocket().getOutputStream();
ByteArrayOutputStream bScrn = new ByteArrayOutputStream();
ImageIO.write(screenshot, "JPG", bScrn);
byte[] imgByte = bScrn.toByteArray();
bScrn.flush();
bScrn.close();
filesize = bScrn.size();
out.write(new String("#FS " + filesize).getBytes()); //Send filesize
out.write(new String("#<IM> \n").getBytes()); //Notify start of image
out.write(imgByte); //Write file
System.out.println("Finished");
Reciever: (where input is the socket input stream)
Attempt #1:
String str = input.toString();
imageBytes = str.getBytes();
InputStream in = new ByteArrayInputStream(imageBytes);
BufferedImage image = ImageIO.read(in);
in.close();
System.out.println("width=" + image.getWidth());
(failed: Nullpointer exception on getWidth() line)
I understand this error to mean "corrupt image" because it couldn't initialize it. correct?
Attempt #2:
byte[] imageBytes = new byte[filesize];
for (int j = 0; i < filesize; i++)
{
imageBytes[j] = (byte) input.read();
}
InputStream in = new ByteArrayInputStream(imageBytes);
BufferedImage image = ImageIO.read(in);
in.close();
System.out.println("width=" + image.getWidth());
(failed: Nullpointer exception on getWidth() line)
Attempt #3:
if (filesize > 0)
{
int writtenBytes = 0;
int bufferSize = client.getReceiveBufferSize();
imageBytes = new byte[filesize]; //Create a byte array as large as the image
byte[] buffer = new byte[bufferSize];//Create buffer
do {
writtenBytes += input.read(buffer); //Fill up buffer
System.out.println(writtenBytes + "/" + filesize); //Show progress
//Copy buffer to the byte array which will contain the full image
System.arraycopy(buffer, 0, imageBytes, writtenBytes, client.getReceiveBufferSize());
writtenBytes+=bufferSize;
} while ((writtenBytes + bufferSize) < filesize);
// Read the remaining bytes
System.arraycopy(buffer, 0, imageBytes, writtenBytes-1, filesize-writtenBytes);
writtenBytes += filesize-writtenBytes;
System.out.println("Finished reading! Total read: " + writtenBytes + "/" + filesize);
}
InputStream in = new ByteArrayInputStream(imageBytes);
BufferedImage image = ImageIO.read(in);
in.close();
(failed: Reciever gives: Null pointer exception)
Attempt 4:
int readBytes = 0;
imageBytes = new byte[filesize]; //Create a byte array as large as the image
while (readBytes < filesize)
{
readBytes += input.read(imageBytes);
}
InputStream in = new ByteArrayInputStream(imageBytes);
BufferedImage image = ImageIO.read(in);
in.close();
System.out.println("width=" + image.getWidth());
(failed: sender gives: java.net.SocketException: Connection reset by peer: socket write error)
Attempt #5:
Using Jon skeet's code snippet, the image arrives, but only partially. I saved it to a file (1.jpg) to see what was going on, and it actually sends 80% of the image, while the rest of the file is filled with blank spaces. This results in a partially corrupt image. Here is the code I tried: (note that captureImg() is not at fault, saving the file directly works)
Sender:
Socket s = new Socket("127.0.0.1", 1290);
OutputStream out = s.getOutputStream();
ByteArrayOutputStream bScrn = new ByteArrayOutputStream();
ImageIO.write(captureImg(), "JPG", bScrn);
byte imgBytes[] = bScrn.toByteArray();
bScrn.close();
out.write((Integer.toString(imgBytes.length)).getBytes());
out.write(imgBytes,0,imgBytes.length);
Reciever:
InputStream in = clientSocket.getInputStream();
long startTime = System.currentTimeMillis();
byte[] b = new byte[30];
int len = in.read(b);
int filesize = Integer.parseInt(new String(b).substring(0, len));
if (filesize > 0)
{
byte[] imgBytes = readExactly(in, filesize);
FileOutputStream f = new FileOutputStream("C:\\Users\\Dan\\Desktop\\Pic\\1.jpg");
f.write(imgBytes);
f.close();
System.out.println("done");
The sender still gives a Connection reset by peer: socket write error.
Click here for full sized image
One option would be to write the image to a ByteArrayOutputStream so you can determine the length, then write that length to the output stream first.
Then on the receiving end, you can read the length, then read that many bytes into a byte array, then create a ByteArrayInputStream to wrap the array and pass that to ImageIO.read().
I'm not entirely surprised that it doesn't work until the output socket is closed normally - after all, a file which contains a valid PNG file and then something else isn't actually a valid PNG file in itself, is it? So the reader needs to read to the end of the stream before it can complete - and the "end" of a network stream only comes when the connection is closed.
EDIT: Here's a method to read the given number of bytes into a new byte array. It's handy to have as a separate "utility" method.
public static byte[] readExactly(InputStream input, int size) throws IOException
{
byte[] data = new byte[size];
int index = 0;
while (index < size)
{
int bytesRead = input.read(data, index, size - index);
if (bytesRead < 0)
{
throw new IOException("Insufficient data in stream");
}
index += bytesRead;
}
return data;
}
for other StackOverflow users like me.
In "Jon Skeet's" answer. Modify the following line of readExactly method.
<<original Line>>
index += size;
<<modified Line>>
index += bytesRead;
To get the full image data.
public static void main(String[] args) {
Socket socket = null;
try {
DataInputStream dis;
socket = new Socket("192.168.1.48",8000);
while (true) {
dis = new DataInputStream(socket.getInputStream());
int len = dis.readInt();
byte[] buffer = new byte[len];
dis.readFully(buffer, 0, len);
BufferedImage im = ImageIO.read(new ByteArrayInputStream(buffer));
jlb.setIcon(new ImageIcon(im));
jfr.add(jlb);
jfr.pack();
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfr.setVisible(true);
System.gc();
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
In 192.168.1.48:8000 machine python server running and i got stream in java code