I have a file containing different tags and datas like:

VERSION: 1.1.1
NAME: xxx
VALUE: 1.5
START_IMAGE
‰PNG....
END_IMAGE

i can't change this file....it is not mine...

But i need to read the png image between the START_IMAGE and END_IMAGE tags. I don't know the length of the image-data.

How can i do this?

Recommended Answers

All 7 Replies

so now i have more time to explain.

I tried it with a scanner:
Scanner in = new Scanner(new FileInputStream(path));

while(in.hasNext()){
String next = in.getNext();
if(next.equals("START_IMAGE")){
//read image???
}
}

how can i read it if i don't know the length?

Read the file line by line.
When the line you have read is equals to: START_IMAGE, then the next lines will be the image. Then when the line read becomes equals to: END_IMAGE then exit the while loop that you had for reading the file:

String line = readLine();
boolean image=false;
while (line!=null) {

if (line.equals("END_IMAGE")) {
  break;
}

if (image) {
 //then the line has info about the image
}

if (line.equals("START_IMAGE")) {
  image=true;
  //the next line will be the image
}

line = readLine();
}

ok, i have tihs part already, thank you anyway...but i need the part to read the image.

readLine() of the BufferedReader and the next() of Scanner return a String...but i need bytes to create a BufferedImage, don't I?

but if i read byte after byte (e.g. DataInputStream) and check if it is the "END_IMAGE"-Tag it is very slow!

do you know a fast way?

Can't you convert what you read as String into a byte?

yes, i can convert it, but aren't the bytes of the String dependent from the character set?

readByte() == readNext().getBytes() ??

I really don't know. Never had to write something like that. Sorry.

i did it this way...it's slow but it works...i am still open for hints or other solutions!

private void readImages(String path) throws IOException {

    // get bytes[] of start and end tags
    byte[] startPattern = prop.getProperty(ExportConst.START_IR_IMAGE)
        .getBytes(Export.CHARSET);
    byte[] endPattern = prop.getProperty(ExportConst.END_IR_IMAGE).getBytes(
        Export.CHARSET);

    FileInputStream fin = null;
    BufferedInputStream dis = null;
    try {
      fin = new FileInputStream(path);
      dis = new BufferedInputStream(fin);

      // if measurement has no image, add an empty one
      for (int im = 0; im < this.availableImages.size(); im++) {
        // buffer with the same length as the searched pattern
        byte[] sbuffer = new byte[startPattern.length];
        // fill the buffer
        dis.read(sbuffer);
        // buffer for the next byte
        byte[] sb = new byte[1];
        // flag to check if pattern was found
        boolean sfound = Arrays.equals(startPattern, sbuffer);
        if (!sfound) {
          // loop over every byte (slow)
          while (dis.read(sb) != -1) {
            // shift the buffer one to the left
            System.arraycopy(sbuffer, 1, sbuffer, 0, sbuffer.length - 1);
            // add the new byte to the buffer
            sbuffer[sbuffer.length - 1] = sb[0];
            // check if the buffer matches to pattern
            if (Arrays.equals(startPattern, sbuffer)) {
              sfound = true;
              break;
            }
          }
        }
        // exit if pattern not found
        if (!sfound) {
          return;
        }
        // buffer for bytes of image
        ArrayList<Byte> image = new ArrayList<Byte>();
        // skip line-seperator
        dis.read(new byte[2]);
        // buffer with the same length as the searched pattern
        byte[] buffer = new byte[endPattern.length];
        // fill the buffer
        dis.read(buffer);
        // buffer for the next byte
        byte[] b = new byte[1];
        // flag to check if pattern was found
        boolean found = Arrays.equals(endPattern, buffer);
        if (!found) {
          // loop over every byte (slow)
          while (dis.read(b) != -1) {
            // add first byte of buffer to image-buffer;
            image.add(buffer[0]);
            // shift the buffer one to the left
            System.arraycopy(buffer, 1, buffer, 0, buffer.length - 1);
            // add the new byte to the buffer
            buffer[buffer.length - 1] = b[0];
            // check if the buffer matches to pattern
            if (Arrays.equals(endPattern, buffer)) {
              found = true;
              break;
            }

          }
          if (found) {
            // convert arraylist to array (toArray() does not work for primitive
            // datatypes)
            byte[] img = new byte[image.size() - 2];
            for (int i = 0; i < image.size() - 2; i++) {
              img[i] = image.get(i);
            }
            // create bufferedimage
            BufferedImage bi = ImageIO.read(ImageIO
                .createImageInputStream(new ByteArrayInputStream(img)));
            // add empty image if bufferedimage is null
            if (bi == null)
              data.getIRimageData().add(
                  new BufferedImage(1000, 800, BufferedImage.TYPE_3BYTE_BGR));
            else
              data.getIRimageData().add(bi);
          }
        }
      }
    }
    finally {
      if (fin != null)
        try {
          fin.close();
        }
        catch (IOException e) {
          // do nothing
        }
      if (dis != null)
        try {
          dis.close();
        }
        catch (IOException e) {
          // do nothing
        }
    }
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.