Question

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top