0

I am trying to get the length of an image as well as the length of a portion of it, but both give the same result. Where did I go wrong?

Part of area of image:

private BufferedImage add_text(BufferedImage image, String text) {
    BufferedImage imageLocation=image.getSubimage(image.getWidth()/2, image.getHeight()/2, 
            image.getWidth()-image.getWidth()/2, image.getHeight()-image.getHeight()/2);

    byte img[] = get_byte_data(imageLocation);
    System.out.println("Image length: "+img.length);

Whole image:

private BufferedImage add_text(BufferedImage image, String text) {

    byte img[] = get_byte_data(image);
    System.out.println("Image length: "+img.length);

The output of both is

Image length: 984900

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
Jarek Huang
  • 119
  • 3
  • 17
  • What is the length of an image? – Justin Jan 15 '14 at 04:27
  • Here you are getting length of the byte array. Not the actual image.You should use imageLocation.getWidth() and imageLocation.getHeight(). – Don Srinath Jan 15 '14 at 04:27
  • What the definition of `get_byte_data`? – jerry Jan 15 '14 at 04:30
  • if you want to get size of a portion of image in bytes in a particular format (lets say JPEG) u need to create a new bitmap copy the contents you want( the portion of the image ) and then store that as byte stream in a MemeoryStream object and then get length. If this is indeed your question – Pulkit Sethi Jan 15 '14 at 04:35
  • Sorry, my poor English. It should be getting length of the image byte array. – Jarek Huang Jan 15 '14 at 04:54

1 Answers1

0

Here you are getting length of the byte array. Not the actual image. You should use imageLocation.getWidth() and imageLocation.getHeight().

If you have checked the java doc http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html it says that getSubimage method shares the same data array as the original image. That is why you are getting same length for the data array length.

Don Srinath
  • 1,565
  • 1
  • 21
  • 32
  • Thank you Srinath. But i have a question, because i want to put a message in this part of area, so i need to exactly location of this area. `private byte[] get_byte_data(BufferedImage image) { WritableRaster raster = image.getRaster();` In this image here is the original image?? or the part of image?? – Jarek Huang Jan 15 '14 at 05:07
  • @JarekHuang , if you just need to print some text on the original image it's much more easier. Check the answer for this question. http://stackoverflow.com/questions/2658554/using-graphics2d-to-overlay-text-on-a-bufferedimage-and-return-a-bufferedimage – Don Srinath Jan 15 '14 at 05:23
  • Actually, i am trying to put the invisible message into the image, but i want to put it to difference part of image. This is a link i am asking [link]http://stackoverflow.com/questions/21127860/put-invisible-message-in-four-part-of-image-by-using-raster – Jarek Huang Jan 15 '14 at 05:54
  • Because i need to know the x - the X coordinate of the upper-left corner of the specified rectangular region y - the Y coordinate of the upper-left corner of the specified rectangular region w - the width of the specified rectangular region h - the height of the specified rectangular region if i just use `imageLocation.getWidth() and imageLocation.getHeight()` , it will not have exactly rectangular region i wanted. – Jarek Huang Jan 15 '14 at 05:56
  • @JarekHuang I think you have mixed up Steganography concepts and Java image manipulation. I'll answer your other question on Steganography since it doesn't relate directly to this question. – Don Srinath Jan 25 '14 at 05:26