Ejemplo n.º 1
1
  // tokenizes a string for PBM and PGM headers and plain PBM and PGM data.  If oneChar is true,
  // then
  // rather than parsing a whole string, a single character is read (not including whitespace or
  // comments)
  static String tokenizeString(InputStream stream, boolean oneChar) throws IOException {
    final int EOF = -1;

    StringBuilder b = new StringBuilder();
    int c;
    boolean inComment = false;

    while (true) {
      c = stream.read();
      if (c == EOF)
        throw new IOException(
            "Stream ended prematurely, before table reading was completed."); // no more tokens
      else if (inComment) {
        if (c == '\r' || c == '\n') // escape the comment
        inComment = false;
        else {
        } // do nothing
      } else if (Character.isWhitespace((char) c)) {
      } // do nothing
      else if (c == '#') // start of a comment
      {
        inComment = true;
      } else // start of a string
      {
        b.append((char) c);
        break;
      }
    }

    if (oneChar) return b.toString();

    // at this point we have a valid string.  We read until whitespace or a #
    while (true) {
      c = stream.read();
      if (c == EOF) break;
      else if (c == '#') // start of comment, read until a '\n'
      {
        while (true) {
          c = stream.read(); // could hit EOF, which is fine
          if (c == EOF) break;
          else if (c == '\r' || c == '\n') break;
        }
        // break;   // comments are not delimiters
      } else if (Character.isWhitespace((char) c)) break;
      else b.append((char) c);
    }
    return b.toString();
  }
Ejemplo n.º 2
1
  /**
   * Performs OCR operation.
   *
   * @param imageList a list of <code>IIOImage</code> objects
   * @param rect the bounding rectangle defines the region of the image to be recognized. A
   *     rectangle of zero dimension or <code>null</code> indicates the whole image.
   * @return the recognized text
   * @throws TesseractException
   */
  public String doOCR(List<IIOImage> imageList, Rectangle rect) throws TesseractException {
    init();
    setTessVariables();

    try {
      StringBuilder sb = new StringBuilder();

      for (IIOImage oimage : imageList) {
        pageNum++;
        try {
          setImage(oimage.getRenderedImage(), rect);
          sb.append(getOCRText());
        } catch (IOException ioe) {
          // skip the problematic image
          logger.log(Level.SEVERE, ioe.getMessage(), ioe);
        }
      }

      if (hocr) {
        sb.insert(0, htmlBeginTag).append(htmlEndTag);
      }

      return sb.toString();
    } finally {
      dispose();
    }
  }
Ejemplo n.º 3
1
 /**
  * Writes the <code>shape</code>, <code>coords</code>, <code>href</code>,
  * <code>nohref</code> Attribute for the specified figure and shape.
  *
  * @return Returns true, if the polygon is inside of the image bounds.
  */
 private boolean writePolyAttributes(IXMLElement elem, SVGFigure f, Shape shape) {
     AffineTransform t = TRANSFORM.getClone(f);
     if (t == null) {
         t = drawingTransform;
     } else {
         t.preConcatenate(drawingTransform);
     }
     
     StringBuilder buf = new StringBuilder();
     float[] coords = new float[6];
     GeneralPath path = new GeneralPath();
     for (PathIterator i = shape.getPathIterator(t, 1.5f);
     ! i.isDone(); i.next()) {
         switch (i.currentSegment(coords)) {
             case PathIterator.SEG_MOVETO :
                 if (buf.length() != 0) {
                     throw new IllegalArgumentException("Illegal shape "+shape);
                 }
                 if (buf.length() != 0) {
                     buf.append(',');
                 }
                 buf.append((int) coords[0]);
                 buf.append(',');
                 buf.append((int) coords[1]);
                 path.moveTo(coords[0], coords[1]);
                 break;
             case PathIterator.SEG_LINETO :
                 if (buf.length() != 0) {
                     buf.append(',');
                 }
                 buf.append((int) coords[0]);
                 buf.append(',');
                 buf.append((int) coords[1]);
                 path.lineTo(coords[0], coords[1]);
                 break;
             case PathIterator.SEG_CLOSE :
                 path.closePath();
                 break;
             default :
                 throw new InternalError("Illegal segment type "+i.currentSegment(coords));
         }
     }
     elem.setAttribute("shape", "poly");
     elem.setAttribute("coords", buf.toString());
     writeHrefAttribute(elem, f);
     return path.intersects(new Rectangle2D.Float(bounds.x, bounds.y, bounds.width, bounds.height));
 }
Ejemplo n.º 4
0
  public void draw(BufferedImage buf) {
    WritableRaster dst = buf.getRaster();
    blit(dst, bg.getRaster(), Coord.z);

    alphablit(dst, rmeter(sbars[0].getRaster(), lmax[0], max), mc[0]);
    alphablit(
        dst, lmeter(sbars[1].getRaster(), lmax[1], max), mc[1].sub(bars[1].getWidth() - 1, 0));
    alphablit(
        dst, lmeter(sbars[2].getRaster(), lmax[2], max), mc[2].sub(bars[2].getWidth() - 1, 0));
    alphablit(dst, rmeter(sbars[3].getRaster(), lmax[3], max), mc[3]);

    if (lfood != null) {
      double e = foodeff(lfood);
      alphablit(dst, rgmeter(lfood, e, 0), mc[0]);
      alphablit(dst, lgmeter(lfood, e, 1), mc[1].sub(bars[1].getWidth() - 1, 0));
      alphablit(dst, lgmeter(lfood, e, 2), mc[2].sub(bars[1].getWidth() - 1, 0));
      alphablit(dst, rgmeter(lfood, e, 3), mc[3]);
    }

    alphablit(dst, rmeter(bars[0].getRaster(), lev[0], max), mc[0]);
    alphablit(dst, lmeter(bars[1].getRaster(), lev[1], max), mc[1].sub(bars[1].getWidth() - 1, 0));
    alphablit(dst, lmeter(bars[2].getRaster(), lev[2], max), mc[2].sub(bars[2].getWidth() - 1, 0));
    alphablit(dst, rmeter(bars[3].getRaster(), lev[3], max), mc[3]);

    StringBuilder tbuf = new StringBuilder();
    for (int i = 0; i < 4; i++)
      tbuf.append(
          String.format(
              "%s: %s/%s\n", rnm[i], Utils.fpformat(lev[i], 3, 1), Utils.fpformat(lmax[i], 3, 1)));
    tooltip = RichText.render(tbuf.toString(), 0).tex();
  }
Ejemplo n.º 5
0
 public static String getLines(String s) {
   StringBuilder sb = new StringBuilder();
   int index = 0;
   for (int i = 1; index != -1; i++) {
     sb.append(i + "\n");
     index = s.indexOf("\n", index + 1);
   }
   return sb.toString();
 }
Ejemplo n.º 6
0
 public static String inputStreamReaderToString(InputStreamReader in) throws Exception {
   StringBuilder sb = new StringBuilder();
   char[] buf = new char[1025];
   while (in.ready()) {
     sb.append((new String(buf, 0, in.read(buf, 0, 1024))).replaceAll("\r\n", "\n"));
   }
   in.close();
   String tmp = sb.toString();
   while (tmp.contains("\r\n")) {
     tmp = tmp.replaceAll("\r\n", "\n");
   }
   return tmp;
 }
Ejemplo n.º 7
0
  private StringBuilder createOutpuFile(
      int numOfTags,
      int[] tags,
      int tagStartIndexForFilename,
      boolean multiple,
      int subImageIndex) {
    final StringBuilder filenameBuilder = new StringBuilder();
    int i = tagStartIndexForFilename;
    while (true) {
      filenameBuilder.append(names[tags[i]]);
      if (++i == numOfTags) {
        break;
      } else {
        filenameBuilder.append('.');
      }
    }

    if (multiple) {
      filenameBuilder.append('-').append(subImageIndex);
    }

    return filenameBuilder.append(".png");
  }