Search in sources :

Example 1 with UnknownRegionType

use of eu.transkribus.core.model.beans.pagecontent.UnknownRegionType in project TranskribusCore by Transkribus.

the class TrpPdfDocument method addPage.

@SuppressWarnings("unused")
public void addPage(URL imgUrl, TrpDoc doc, PcGtsType pc, boolean addAdditionalPlainTextPage, boolean imageOnly, FimgStoreImgMd md, boolean doBlackening, ExportCache cache) throws MalformedURLException, IOException, DocumentException, JAXBException, URISyntaxException {
    imgOnly = imageOnly;
    extraTextPage = addAdditionalPlainTextPage;
    // FIXME use this only on cropped (printspace) images!!
    java.awt.Rectangle printspace = null;
    // if(pc.getPage() != null && pc.getPage().getPrintSpace() != null){
    // java.awt.Polygon psPoly = PageXmlUtils.buildPolygon(pc.getPage().getPrintSpace().getCoords());
    // printspace = psPoly.getBounds();
    // }
    BufferedImage imgBuffer = null;
    try (InputStream input = imgUrl.openStream()) {
        imgBuffer = ImageIO.read(input);
    } catch (FileNotFoundException e) {
        logger.error("File was not found at url " + imgUrl);
        URL origUrl = new URL(imgUrl.getProtocol(), imgUrl.getHost(), imgUrl.getFile().replace("view", "orig"));
        logger.debug("try orig file location " + origUrl);
        try (InputStream input = origUrl.openStream()) {
            imgBuffer = ImageIO.read(input);
        }
    }
    Graphics2D graph = imgBuffer.createGraphics();
    graph.setColor(Color.BLACK);
    List<TrpRegionType> regions = pc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion();
    // regions should be sorted after their reading order at this point - so no need to resort
    // Collections.sort(regions, new TrpElementCoordinatesComparator<RegionType>());
    int nrOfTextRegions = 0;
    for (RegionType r : regions) {
        // used later to decide if new page is necessary if there is at least one text region
        if (r instanceof TextRegionType) {
            nrOfTextRegions++;
        } else if (r instanceof UnknownRegionType && doBlackening) {
            UnknownRegionType urt = (UnknownRegionType) r;
            ITrpShapeType trpShape = (ITrpShapeType) r;
            boolean isBlackening = RegionTypeUtil.isBlackening(trpShape);
            if (isBlackening) {
                // Rectangle blackRect = (Rectangle) PageXmlUtils.buildPolygon(urt.getCoords().getPoints()).getBounds();
                Rectangle blackRect = urt.getBoundingBox();
                graph.fillRect((int) blackRect.getMinX(), (int) blackRect.getMinY(), (int) blackRect.getWidth(), (int) blackRect.getHeight());
            }
        }
    }
    graph.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(imgBuffer, "JPEG", baos);
    byte[] imageBytes = baos.toByteArray();
    Image img = Image.getInstance(imageBytes);
    baos.close();
    imgBuffer.flush();
    imgBuffer = null;
    /*
		 * take resolution from metadata of image store, values in img are not always set
		 */
    if (md != null) {
        double resolutionX = (float) md.getXResolution();
        double resolutionY = (float) md.getYResolution();
        // logger.debug("Dpi: " + md.getXResolution());
        img.setDpi((int) resolutionX, (int) resolutionY);
    }
    // else{
    // 
    // Image img = Image.getInstance(imgUrl);
    // }
    int cutoffLeft = 0;
    int cutoffTop = 0;
    if (printspace == null) {
        /*
			 * 1 Punkt pro cm  = 2,54 dpi
			 * img.getPlainWidth() = horizontal size in Pixel
			 * img.getPlainHeight() = vertical size in Pixel
			 * img.getDpiX() = resolution of x direction
			 * Size in cm: img.getDpiX() / (img.getDpiX()/2,54)
			 */
        // logger.debug("Horizontal size in cm: img.getPlainWidth() / (img.getDpiX()/2,54): " + img.getPlainWidth() / (img.getDpiX()/2.54));
        // logger.debug("Vertical size in cm: img.getPlainHeight() / (img.getDpiY()/2,54): " + img.getPlainHeight() / (img.getDpiY()/2.54));
        setPageSize(img);
    } else {
        int width = (int) printspace.getWidth();
        int height = (int) printspace.getHeight();
        setPageSize(new com.itextpdf.text.Rectangle(width, height));
        cutoffLeft = printspace.x;
        cutoffTop = printspace.y;
    }
    float xSize;
    float ySize;
    /*
		 * calculate size of image with respect to Dpi of the image and the default points of PDF which is 72
		 * PDF also uses the same basic measurement unit as PostScript: 72 points == 1 inch
		 */
    if (img.getDpiX() > 72f) {
        xSize = (float) (img.getPlainWidth() / img.getDpiX() * 72);
        ySize = (float) (img.getPlainHeight() / img.getDpiY() * 72);
        scaleFactorX = scaleFactorY = (float) (72f / img.getDpiX());
    } else {
        xSize = (float) (img.getPlainWidth() / 300 * 72);
        ySize = (float) (img.getPlainHeight() / 300 * 72);
        scaleFactorX = scaleFactorY = 72f / 300;
    }
    /*
		 * construct the grid for the added page
		 */
    for (int i = 0; i <= 12; i++) {
        twelfthPoints[i][0] = i * (img.getPlainWidth() / 12);
        twelfthPoints[i][1] = i * (img.getPlainHeight() / 12);
    }
    // TODO use scaleToFit instead?
    img.scaleAbsolute(xSize, ySize);
    img.setAbsolutePosition(0, 0);
    /*
		 * calculate physical size of image in inch and assign text size dependent on these values
		 */
    if (img.getScaledWidth() / 72f < 9 && img.getScaledHeight() / 72f < 12) {
        lineMeanHeight = 12 / scaleFactorY;
    } else {
        lineMeanHeight = 17 / scaleFactorY;
    }
    if (doc != null && createTitle) {
        addTitlePage(doc);
        // logger.debug("page number " + getPageNumber());
        if (getPageNumber() % 1 != 0) {
            logger.debug("odd page number -> add one new page");
            document.newPage();
            // necessary that an empty page can be created
            writer.setPageEmpty(false);
        }
    }
    document.newPage();
    addTextAndImage(pc, cutoffLeft, cutoffTop, img, imageOnly, cache);
    if (addAdditionalPlainTextPage) {
        if (nrOfTextRegions > 0) {
            logger.debug("add uniform text");
            document.newPage();
            addUniformText(pc, cutoffLeft, cutoffTop, cache);
        }
    }
}
Also used : Rectangle(java.awt.Rectangle) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) UnknownRegionType(eu.transkribus.core.model.beans.pagecontent.UnknownRegionType) TrpRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType) TrpTableRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTableRegionType) RegionType(eu.transkribus.core.model.beans.pagecontent.RegionType) TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Rectangle(java.awt.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) Image(com.itextpdf.text.Image) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) Point(java.awt.Point) ITrpShapeType(eu.transkribus.core.model.beans.pagecontent_trp.ITrpShapeType) Graphics2D(java.awt.Graphics2D) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) UnknownRegionType(eu.transkribus.core.model.beans.pagecontent.UnknownRegionType) TrpRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType)

Aggregations

Image (com.itextpdf.text.Image)1 RegionType (eu.transkribus.core.model.beans.pagecontent.RegionType)1 TextRegionType (eu.transkribus.core.model.beans.pagecontent.TextRegionType)1 UnknownRegionType (eu.transkribus.core.model.beans.pagecontent.UnknownRegionType)1 ITrpShapeType (eu.transkribus.core.model.beans.pagecontent_trp.ITrpShapeType)1 TrpRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType)1 TrpTableRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTableRegionType)1 TrpTextRegionType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType)1 Graphics2D (java.awt.Graphics2D)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1