How to convert a Image to String? and then back

The Image only exist in the memory, not as a file on the comp. I was thinking of making a function like. I thought this could be useful to storing images right in the code or sending images over the internet.

[code]public String ImageToString (Image img1){
String str1="";

for(int y=0; y<img1.getHeight(); y++){
for(int x=0; x<img1.getWidth(); x++){
str1+=(String)img1.getPixel(x,y);
}
}
return str1;
}[/code]
Maybee theres a built in function for this?
Or maybee i should try a BufferedInputStream IO dunno how to use thoose though. For net programming ive only used Strings so far.

I stumbled upon this
http://www.java-forums.org/networking/4284-server-socket-send-image-client.html
It says how he can send an image over the network. Sounds good to me just that my image is not in a file on the computer. It's a screenshot. I could first store it in file and then use that example to send it but it seems rather inefficient.

More browsing later this guy wants exactly what i want
http://www.daniweb.com/forums/thread62191.html
The answers where rather unhelpful though.

Dont really know what to look for. I'm just guessing the code right now.

How do i get byte data from an image?

Comments

  • : The Image only exist in the memory, not as a file on the comp. I was
    : thinking of making a function like. I thought this could be useful
    : to storing images right in the code or sending images over the
    : internet.
    :
    : [code]: public String ImageToString (Image img1){
    : String str1="";
    :
    : for(int y=0; y<img1.getHeight(); y++){
    : for(int x=0; x<img1.getWidth(); x++){
    : str1+=(String)img1.getPixel(x,y);
    : }
    : }
    : return str1;
    : }[/code]:
    : Maybee theres a built in function for this?
    : Or maybee i should try a BufferedInputStream IO dunno how to use
    : thoose though. For net programming ive only used Strings so far.
    :
    : I stumbled upon this
    : http://www.java-forums.org/networking/4284-server-socket-send-image-c
    : lient.html
    : It says how he can send an image over the network. Sounds good to me
    : just that my image is not in a file on the computer. It's a
    : screenshot. I could first store it in file and then use that example
    : to send it but it seems rather inefficient.
    :
    : More browsing later this guy wants exactly what i want
    : http://www.daniweb.com/forums/thread62191.html
    : The answers where rather unhelpful though.
    :
    : Dont really know what to look for. I'm just guessing the code right
    : now.
    :
    : How do i get byte data from an image?

    The easiest way is to store the image in a BufferedImage (http://java.sun.com/javase/6/docs/api/java/awt/image/BufferedImage.html). They you can use getRGB() to get the individual pixels.
    Those integers then need to be stored in an string, for which you can use the Integer.toHexString() method. Be careful. This function returns variable length strings for each integer, so you need to add padding to align them properly, or use some delimiter to separate them.
    Finally concatenate the strings together to one big string.

    To build your image from the string, reverse the process:
    - separate the individual integers (as strings) from your string
    - Convert the strings to integers (see Integer.parseInt(String, int))
    - use BufferedImage.setRGB() to place the pixels onto the image.
  • : The Image only exist in the memory, not as a file on the comp. I was
    : thinking of making a function like. I thought this could be useful
    : to storing images right in the code or sending images over the
    : internet.
    :
    : [code]: public String ImageToString (Image img1){
    : String str1="";
    :
    : for(int y=0; y<img1.getHeight(); y++){
    : for(int x=0; x<img1.getWidth(); x++){
    : str1+=(String)img1.getPixel(x,y);
    : }
    : }
    : return str1;
    : }[/code]:
    : Maybee theres a built in function for this?
    : Or maybee i should try a BufferedInputStream IO dunno how to use
    : thoose though. For net programming ive only used Strings so far.
    :
    : I stumbled upon this
    : http://www.java-forums.org/networking/4284-server-socket-send-image-c
    : lient.html
    : It says how he can send an image over the network. Sounds good to me
    : just that my image is not in a file on the computer. It's a
    : screenshot. I could first store it in file and then use that example
    : to send it but it seems rather inefficient.
    :
    : More browsing later this guy wants exactly what i want
    : http://www.daniweb.com/forums/thread62191.html
    : The answers where rather unhelpful though.
    :
    : Dont really know what to look for. I'm just guessing the code right
    : now.
    :
    : How do i get byte data from an image?

    Check out here
    [link=http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx]Convert Image to Base64 String and Base64 String to Image[/link]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion