Jump to content

Creating Image From Byte Array


JacquesLemaire

Recommended Posts

Hello,

 

I am trying to get an image from a java server. When I try to display the image, nothing shows. I get the byte array from a socket connect to the java server.

 

Php code from Communication_socket.php file:

 

<?php
  //header('Content-Type: image/png');

  $PORT = 50898; //the port on which we are connecting to the "remote" machine
  $HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine)
  $sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket
	  or die("error: could not create socket\n");
  $succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket
	  or die("error: could not connect to host\n");
  $reply = socket_read($sock, 10000, PHP_BINARY_READ) //Reading the reply from socket

	 or die("error: failed to read from socket\n");
  echo $reply;
?>

and in the other php file:
<html>
<?php include 'includes/head.php'; ?>
<body>
<div id="page">
	<?php include 'includes/header.php'; ?>
  <img src="Communication_socket.php" alt="" border = "5">
</body>
</html>

And the java code where i'm sending the byte array

import java.io.*;
import java.net.*;
import java.lang.*;
import java.io.File;
import java.text.*;
import java.util.*;


public class Serveur {
  DataOutputStream out = null;
  public void lancerServeur()
  {


	 Socket socket = null;
	 ServerSocket socketServeur = null;

	 PrintWriter writer = null;

	 try
	 {

		socketServeur = new ServerSocket(50898);
		System.out.println("attente d'une connexion");

		socket = socketServeur.accept();
		out = new DataOutputStream(socket.getOutputStream());
		writer = new PrintWriter(
					new OutputStreamWriter(
					   socket.getOutputStream()));
		System.out.println("client connecte");

		File file = new File("image.jpg");
		FileInputStream fin = new FileInputStream(file);

		byte []fileContent = new byte[(int)file.length()];

		fin.read(fileContent);
		System.out.println(fileContent);
		out.write(fileContent);
		out.close();
	 }
	 catch (IOException e)
	 {}
  }
  public static void main(String[] args) {
  Serveur app = new Serveur();
  app.lancerServeur();
  }
}

What can I do? I tried a lot of things but nothing worker out...

 

Thanks!

Edited by Zane
Link to comment
Share on other sites

<img src="Communication_socket.php" alt="" border = "5">

php is not an image file.

 

You can put a PHP file as the source for an image, there is nothing wrong with that.  You just have to make sure the PHP file outputs a valid image file.

I assume you are trying to display $reply? if so use require() then echo $reply into the img src

No, you don't just echo raw image data into the src attribute.  At the very least you would have to construct a data: uri from the data, but using a separate PHP file as the OP is currently doing would be the easiest/most flexable method.

 

 

What can I do? I tried a lot of things but nothing worker out...

 

Load your Communication_socket.php file directly in the browser so you can see if there is any extra output such as PHP warnings or HTML code.  If there is any output other than the image data you receive it will cause the image to be invalid.

Link to comment
Share on other sites

You have the 'Content-Type' header call commented out. The PHP script will have to output headers appropriate for the image, so the browser will recognize it.

 

If you tried to browse to the communication_socket.php script, how would the browser know it was supposed to be an image? Are you sure you did that correctly? What does the View Source of your browser show when you browse to that script?

Link to comment
Share on other sites

I've never worked with sockets (directly) and don't have much experience with Java, but I'll take a swing at this.

 

$reply = socket_read($sock, 10000, PHP_BINARY_READ) //Reading the reply from socket
                or die("error: failed to read from socket\n");

 

You are asking for 10,000 bytes. Are you sure the image file is not more than that?

 

socket_read returns false on error (which you have covered) or an empty string if there is nothing to read. Check the strlen of the returned value $reply and the file.length() of the image you are reading in Java. They should be the same value.

 

The header in your original post says "PNG" while the Java code is reading a "JPG". Was this more testing? The Content-Type needs to be correct.

 

That error would indicate that the browser is not getting valid image data. Either it is missing some data, or the data is wrong. Could this be a big-indian, little-indian issue?

 

Did you turn on error reporting when you tested the script directly? If you request the script in the browser, without the header call, you should see the image data. Make sure error reporting is turned on and try with and without the header. Are you certain there is no white-space or a UTF-8 BOM before the first opening tag? i.e. does the error reporting give a "Headers already sent" message?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.