private int calculateHeight() { switch (location.location()) { case TOP_LEFT: case TOP_CENTER: case TOP_RIGHT: return imageGrownBy.top; case CENTER_LEFT: case CENTER: case CENTER_RIGHT: return 0; case BOTTOM_LEFT: case BOTTOM_CENTER: case BOTTOM_RIGHT: return imageGrownBy.bottom; } return 0; }
/** * Writes the base image to the systems clipboard. * * @return this. */ @NotNull public ImageOverlayBuilder toClipboard() { new CopyImagetoClipBoard().copyImage(bufferedImage); return this; }
public boolean isDataFlavorSupported(DataFlavor flavor) { DataFlavor[] flavors = getTransferDataFlavors(); for (DataFlavor flavor1 : flavors) { if (flavor.equals(flavor1)) { return true; } } return false; } }
private void applyText(Graphics2D graphics2D) { graphics2D.setFont(requestedFont); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); int availableWidthForTextContainer = boundedTextContainerWidth(graphics2D); setupFontWidth(graphics2D, availableWidthForTextContainer); int availableHeightForTextContainer = boundedTextContainerHeight(graphics2D); setupFontHeight(graphics2D, availableWidthForTextContainer, availableHeightForTextContainer); int startingX = calculateTextContainerStartingPointX(availableWidthForTextContainer); int startingY = calculateTextContainerStartingPointY(baseRectangle, availableHeightForTextContainer, rectangleTextPosition); Rectangle textContainer = new Rectangle(startingX, startingY, availableWidthForTextContainer, availableHeightForTextContainer); drawOutliningRectangle(graphics2D, textContainer, cognitiveJColourPalette.getBackground()); applyTextOver(graphics2D, textContainer); }
@NotNull @Override public BufferedImage applyFilter(@NotNull BufferedImage bufferedImage) { Graphics2D graphics2D = bufferedImage.createGraphics(); imageGrownBy = calculateGrowth(); GrowImageFilter growImageFilter = new GrowImageFilter(imageGrownBy); BufferedImage grownImage = growImageFilter.applyFilter(bufferedImage); int height = calculateHeight(); ImageCaptionFilter imageCaptionFilter = new ImageCaptionFilter(location, OverlayUtils.calculateMaxFontForString(graphics2D, font, grownImage.getWidth() - magicPadding, height, text), color.getBackground(), text, 10); graphics2D.dispose(); return imageCaptionFilter.applyFilter(grownImage); }
private static void buildGrid(ImageOverlayBuilder imageOverlayBuilder, People people) { ImageGrid candidates = new ImageGrid(extractImagesFromPeople(people), 4); ApplyCaptionOutsideImageFilter applyCaptionOutsideImageFilter = new ApplyCaptionOutsideImageFilter(PointLocations.TOP_CENTER, ImageOverlayBuilder.DEFAULT_TEXT_FONT, CognitiveJColourPalette.WHITE, "Candidate Dataset"); BufferedImage mergeImage = applyCaptionOutsideImageFilter.applyFilter(candidates.build()); imageOverlayBuilder.mergeImage(mergeImage); }
@NotNull @Override public BufferedImage applyFilter(@NotNull BufferedImage bufferedImage) { Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setColor(color.getBackground()); graphics2D.setStroke(new BasicStroke(lineThickness.thickness())); Point leftRectangleBottom = new Point(rectangle1.x + rectangle1.width, rectangle1.y + rectangle1.height); Point leftRectangleTop = new Point(rectangle1.x + rectangle1.width, rectangle1.y); Point rightRectangleTop = new Point(rectangle2.x + xOffset, rectangle2.y + insets.top); Point rightRectangleBottom = new Point(rectangle2.x + xOffset, rectangle2.y + insets.top + rectangle2.height); PointDistance pointDistance = calculatePoints(leftRectangleTop, leftRectangleBottom, rightRectangleTop, rightRectangleBottom); graphics2D.drawLine(pointDistance.getLeftPoint().x, pointDistance.getLeftPoint().y, pointDistance.getRightPoint().x, pointDistance.getRightPoint().y); graphics2D.dispose(); return bufferedImage; }
@NotNull @Override public BufferedImage applyFilter(@NotNull BufferedImage bufferedImage) { Graphics2D graphics2D = bufferedImage.createGraphics(); if (rectangleType.equals(RectangleType.CORNERED)) drawCornerRectangle(graphics2D, color.getBackground(), borderWeight, rectangle); else drawRectangle(graphics2D, rectangle, color.getBackground(), borderWeight, false); graphics2D.dispose(); return bufferedImage; }
/** * Outlines face on the base image with defaults; * ({@link ImageOverlayBuilder#outlineFaceOnImage(Face, RectangleType, BorderWeight, CognitiveJColourPalette)} * * @param face - the faces. * @param rectangleType - the rectangle type. * @param borderWeight - the weight of the border * @return this */ @NotNull public ImageOverlayBuilder outlineFaceOnImage(@NotNull Face face, @NotNull RectangleType rectangleType, @NotNull BorderWeight borderWeight) { return outlineFaceOnImage(face, rectangleType, borderWeight, CognitiveJColourPalette.randomColour()); }
/** * write out the faces gender and age (formatted '%Gender, Age %.1f') to the top of the face rectangle. * ({@link ImageOverlayBuilder#outlineFaceOnImage(Face, RectangleType, BorderWeight, CognitiveJColourPalette)} * * @param face - the face. * @return this */ @NotNull public ImageOverlayBuilder writeGenderAndAge(@NotNull Face face) { return writeGenderAndAge(face, CognitiveJColourPalette.randomColour()); }
/** * write out the faces age (formatted 'Age %.1f') to the top of the face rectangle. * ({@link ImageOverlayBuilder#outlineFaceOnImage(Face, RectangleType, BorderWeight, CognitiveJColourPalette)} * * @param face the face. * @param textPosition location of the text boundary * @return this */ @NotNull public ImageOverlayBuilder writeAge(@NotNull Face face, @NotNull RectangleTextPosition textPosition) { return writeAge(face, CognitiveJColourPalette.randomColour(), textPosition); }
/** * write out the faces gender and age (formatted '%Gender, Age %.1f') to the top of the face rectangle. * ({@link ImageOverlayBuilder#outlineFaceOnImage(Face, RectangleType, BorderWeight, CognitiveJColourPalette)} * * @param face the face. * @param colourPalette the color to paint the text * @param borderWeight borderWeight to use * @return this */ @NotNull public ImageOverlayBuilder writeGenderAndAge(@NotNull Face face, @NotNull CognitiveJColourPalette colourPalette, @NotNull BorderWeight borderWeight) { return writeGenderAndAge(face, colourPalette, borderWeight.insets()); }
@NotNull @Override public BufferedImage applyFilter(@NotNull BufferedImage bufferedImage) { Graphics2D graphics2D = bufferedImage.createGraphics(); rectangle.stream().filter(point -> point != null).forEach(point -> drawRectangle(graphics2D, new Rectangle(point.x, point.y, borderWeight.thickness(), borderWeight.thickness()), color.getBackground(), borderWeight)); graphics2D.dispose(); return bufferedImage; }
@NotNull private ImageOverlayBuilder outlineOCRLine(OCRResult.Line line) { OverlayRectangleFilter overlayRectangleFilter = new OverlayRectangleFilter(line.boundingBoxAsAwtRectangle(), RectangleType.FULL, BorderWeight.THINNER, CognitiveJColourPalette.randomColour()); bufferedImage = overlayRectangleFilter.applyFilter(bufferedImage); line.words.forEach(this::outlineOCRWord); return this; }
private void applyTextOver(Graphics2D graphics2D, Rectangle boundedTextContainer) { graphics2D.setColor(cognitiveJColourPalette.getForeground()); java.util.List<String> stringRows = Arrays.asList(text.split("\\n")); int width = boundedTextContainer.width; int height = boundedTextContainer.height; int textWidth = graphics2D.getFontMetrics().stringWidth(maxSingleLineStringWidth(text)); int textHeight = graphics2D.getFontMetrics().getHeight() / 2; int rowPadding = Math.max(0, (height - textHeight * stringRows.size()) / stringRows.size()); Point insideContainerPoint = PointLocations.TOP_CENTER.calculate( width, height, textWidth, 0, 0, 0, 0, 0 ); double yRatio = insideContainerPoint.y / (double) height; int yOffset = (int) ((1.0 - yRatio) * textHeight); for (int i = 0; i < stringRows.size(); i++) { String s = stringRows.get(i); graphics2D.drawString(s, insideContainerPoint.x + boundedTextContainer.x, insideContainerPoint.y + (rowPadding / 2) + yOffset + boundedTextContainer.y + ((textHeight + rowPadding) * i)); } }
private void drawRectangle(Graphics2D workingGraphics, Rectangle rectangle, Color color, BorderWeight borderWeight) { workingGraphics.setColor(color); workingGraphics.setStroke(new BasicStroke(borderWeight.thickness())); workingGraphics.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); }
public void copyImage(BufferedImage bi) { try { TransferableImage trans = new TransferableImage(bi); Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard(); c.setContents(trans, this); } catch (Exception x) { x.printStackTrace(); System.exit(1); } }
/** * Outlines face(s) on the base image with defaults; * ({@link ImageOverlayBuilder#outlineFaceOnImage(Face, RectangleType, BorderWeight, CognitiveJColourPalette)} * * @param faces the faces to outline. * @return this */ @NotNull public ImageOverlayBuilder outlineFacesOnImage(@NotNull List<Face> faces) { faces.forEach(face -> outlineFaceOnImage(face, RectangleType.FULL, DEFAULT_BORDER_WEIGHT, CognitiveJColourPalette.randomColour())); return this; }
private void drawRectangle(Graphics2D workingGraphics, Rectangle rectangle, Color color, BorderWeight borderWeight, boolean fillRect) { workingGraphics.setColor(color); workingGraphics.setStroke(new BasicStroke(borderWeight.thickness())); if (fillRect) workingGraphics.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); else workingGraphics.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); }
public void drawCornerRectangle(Graphics2D workingGraphics, Color color, BorderWeight borderWeight, Rectangle rectangle) { workingGraphics.setColor(color); workingGraphics.setStroke(new BasicStroke(borderWeight.thickness())); int cornerLength = rectangle.width / 6; if ((rectangle.width < 0) || (rectangle.height < 0)) { return; } //left up corner workingGraphics.drawLine(rectangle.x, rectangle.y, rectangle.x + cornerLength, rectangle.y); workingGraphics.drawLine(rectangle.x, rectangle.y, rectangle.x, rectangle.y + cornerLength); //right up corner workingGraphics.drawLine(rectangle.x + rectangle.width, rectangle.y, (rectangle.x + rectangle.width) - cornerLength, rectangle.y); workingGraphics.drawLine(rectangle.x + rectangle.width, rectangle.y, rectangle.x + rectangle.width, rectangle.y + cornerLength); //left down corner workingGraphics.drawLine(rectangle.x, rectangle.y + rectangle.height, rectangle.x + cornerLength, rectangle.y + rectangle.height); workingGraphics.drawLine(rectangle.x, rectangle.y + rectangle.height, rectangle.x, (rectangle.y + rectangle.height) - cornerLength); //right down corner workingGraphics.drawLine(rectangle.x + rectangle.width, rectangle.y + rectangle.height, (rectangle.x + rectangle.width) - cornerLength, rectangle.y + rectangle.height); workingGraphics.drawLine(rectangle.x + rectangle.width, rectangle.y + rectangle.height, rectangle.x + rectangle.width, (rectangle.y + rectangle.height) - cornerLength); }