• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

image to text file and text file to image

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

Would somebody help me with the code to convert image to text file using java.. and again reconstruct the image with the text file as input.??
please help me.. I am struck..

thanks in advance
 
Bartender
Posts: 11188
89
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried so far?

Sounds like you want OCR (Optical Character Recognition) to convert an image to text. There are already programs for that and it would be very difficult to write in Java (or any other language for that matter).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sangeeth priya wrote:and again reconstruct the image with the text file as input.??


What reconstruct the image exactly as you received it? That sounds highly unlikely unless there are some very specific things you haven't told us about this "image".

Winston
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you mean to encode an image binary as text? Then take a look at Base64
 
Sangeeth priya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply..
I need java code to convert image into text and the text into image..

i am trying with this code.
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;

public class ImageTest {

public static void main(String[] args) {

try {

byte[] imageInByte;
BufferedImage originalImage = ImageIO.read(new File(
"a1.jpg"));

// convert BufferedImage to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);



baos.flush();
imageInByte = baos.toByteArray();
System.out.println("The encoded image byte array ============ "+imageInByte );
baos.close();

// convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);

ImageIO.write(bImageFromConvert, "jpg", new File(
"new-darksouls.jpg"));

} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
this works properly.


but when i give the conversion of byte array back to image like this:

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;

public class ImageTest2 {

public static void main(String[] args) {

try {



// convert byte array back to BufferedImage
byte[] imageInByte1 = new byte[] {'[','B','@','1','c','a','1','a','6','8'};
InputStream in = new ByteArrayInputStream(imageInByte1);


BufferedImage bImageFromConvert = ImageIO.read(in);

ImageIO.write(bImageFromConvert, "jpg", new File(
"new-darksouls12.jpg"));

}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

i am getting null pointer exception at the line :
ImageIO.write(bImageFromConvert, "jpg", new File(
"new-darksouls12.jpg"));

if this code is wrong please suggest me the proper one. I am a beginner in this part
thanks
 
Carey Brown
Bartender
Posts: 11188
89
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Pawel mentioned, you'll probably want to use base64 to encode/decode binary data.

First off, you haven't made it clear why you want text. Why not just copy the image file as binary data?

So, assuming text is what you want, base64 will encode binary data (in your case an image) into text data. There's no need to use BufferedImage, you already have an image file (.jpg) in binary format, just read the file and encode it, it doesn't matter if the binary data represents an image or not.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic