How to convert a Image to String? and then back - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to convert a Image to String? and then back

ColacXColacX Posts: 141Member
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

  • zibadianzibadian Posts: 6,349Member
    : 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.
  • rsonirsoni Posts: 1Member
    : 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.