Search in sources :

Example 36 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project ofbiz-framework by apache.

the class QRCodeServices method generateQRCodeImage.

/**
 * Streams QR Code to the result.
 */
public static Map<String, Object> generateQRCodeImage(DispatchContext ctx, Map<String, Object> context) {
    Locale locale = (Locale) context.get("locale");
    String message = (String) context.get("message");
    Integer width = (Integer) context.get("width");
    Integer height = (Integer) context.get("height");
    String format = (String) context.get("format");
    String encoding = (String) context.get("encoding");
    Boolean verifyOutput = (Boolean) context.get("verifyOutput");
    String logoImage = (String) context.get("logoImage");
    Integer logoImageMaxWidth = (Integer) context.get("logoImageMaxWidth");
    Integer logoImageMaxHeight = (Integer) context.get("logoImageMaxHeight");
    if (UtilValidate.isEmpty(message)) {
        return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ParameterCannotEmpty", new Object[] { "message" }, locale));
    }
    if (width == null) {
        width = Integer.parseInt(QRCODE_DEFAULT_WIDTH);
    }
    if (width.intValue() < MIN_SIZE || width.intValue() > MAX_SIZE) {
        return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "SizeOutOfBorderError", new Object[] { "width", String.valueOf(width), String.valueOf(MIN_SIZE), String.valueOf(MAX_SIZE) }, locale));
    }
    if (height == null) {
        height = Integer.parseInt(QRCODE_DEFAULT_HEIGHT);
    }
    if (height.intValue() < MIN_SIZE || height.intValue() > MAX_SIZE) {
        return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "SizeOutOfBorderError", new Object[] { "height", String.valueOf(height), String.valueOf(MIN_SIZE), String.valueOf(MAX_SIZE) }, locale));
    }
    if (UtilValidate.isEmpty(format)) {
        format = QRCODE_DEFAULT_FORMAT;
    }
    if (!FORMATS_SUPPORTED.contains(format)) {
        return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorFormatNotSupported", new Object[] { format }, locale));
    }
    Map<EncodeHintType, Object> encodeHints = null;
    if (UtilValidate.isNotEmpty(encoding)) {
        encodeHints = new EnumMap<>(EncodeHintType.class);
        encodeHints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(message, BarcodeFormat.QR_CODE, width, height, encodeHints);
        BufferedImage bufferedImage = toBufferedImage(bitMatrix, format);
        BufferedImage logoBufferedImage = null;
        if (UtilValidate.isNotEmpty(logoImage)) {
            Map<String, Object> logoImageResult;
            try {
                logoImageResult = ImageTransform.getBufferedImage(FileUtil.getFile(logoImage).getAbsolutePath(), locale);
                logoBufferedImage = (BufferedImage) logoImageResult.get("bufferedImage");
            } catch (IllegalArgumentException | IOException e) {
                Debug.logError(e, module);
            }
        }
        if (UtilValidate.isEmpty(logoBufferedImage)) {
            logoBufferedImage = defaultLogoImage;
        }
        BufferedImage newBufferedImage = null;
        if (UtilValidate.isNotEmpty(logoBufferedImage)) {
            if (UtilValidate.isNotEmpty(logoImageMaxWidth) && UtilValidate.isNotEmpty(logoImageMaxHeight) && (logoBufferedImage.getWidth() > logoImageMaxWidth.intValue() || logoBufferedImage.getHeight() > logoImageMaxHeight.intValue())) {
                Map<String, String> typeMap = new HashMap<>();
                typeMap.put("width", logoImageMaxWidth.toString());
                typeMap.put("height", logoImageMaxHeight.toString());
                Map<String, Map<String, String>> dimensionMap = new HashMap<>();
                dimensionMap.put("QRCode", typeMap);
                Map<String, Object> logoImageResult = ImageTransform.scaleImage(logoBufferedImage, (double) logoBufferedImage.getWidth(), (double) logoBufferedImage.getHeight(), dimensionMap, "QRCode", locale);
                logoBufferedImage = (BufferedImage) logoImageResult.get("bufferedImage");
            }
            BitMatrix newBitMatrix = bitMatrix.clone();
            newBufferedImage = toBufferedImage(newBitMatrix, format);
            Graphics2D graphics = newBufferedImage.createGraphics();
            graphics.drawImage(logoBufferedImage, new AffineTransformOp(AffineTransform.getTranslateInstance(1, 1), null), (newBufferedImage.getWidth() - logoBufferedImage.getWidth()) / 2, (newBufferedImage.getHeight() - logoBufferedImage.getHeight()) / 2);
            graphics.dispose();
        }
        if (UtilValidate.isNotEmpty(verifyOutput) && verifyOutput.booleanValue()) {
            Decoder decoder = new Decoder();
            Map<DecodeHintType, Object> decodeHints = new EnumMap<>(DecodeHintType.class);
            decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            if (UtilValidate.isNotEmpty(encoding)) {
                decodeHints.put(DecodeHintType.CHARACTER_SET, encoding);
            }
            DetectorResult detectorResult = null;
            if (UtilValidate.isNotEmpty(newBufferedImage)) {
                BitMatrix newBitMatrix = createMatrixFromImage(newBufferedImage);
                DecoderResult result = null;
                try {
                    detectorResult = new Detector(newBitMatrix).detect(decodeHints);
                    result = decoder.decode(detectorResult.getBits(), decodeHints);
                } catch (ChecksumException | FormatException | NotFoundException e) {
                    Debug.logError(e, module);
                }
                if (UtilValidate.isNotEmpty(result) && !result.getText().equals(message)) {
                    detectorResult = new Detector(bitMatrix).detect(decodeHints);
                    result = decoder.decode(detectorResult.getBits(), decodeHints);
                    if (!result.getText().equals(message)) {
                        return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "GeneratedTextNotMatchOriginal", new Object[] { result.getText(), message }, locale));
                    }
                } else {
                    bufferedImage = newBufferedImage;
                }
            } else {
                detectorResult = new Detector(bitMatrix).detect(decodeHints);
                DecoderResult result = decoder.decode(detectorResult.getBits(), decodeHints);
                if (!result.getText().equals(message)) {
                    return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "GeneratedTextNotMatchOriginal", new Object[] { result.getText(), message }, locale));
                }
            }
        } else if (UtilValidate.isNotEmpty(newBufferedImage)) {
            bufferedImage = newBufferedImage;
        }
        Map<String, Object> result = ServiceUtil.returnSuccess();
        result.put("bufferedImage", bufferedImage);
        return result;
    } catch (WriterException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorGenerateQRCode", new Object[] { e.toString() }, locale));
    } catch (ChecksumException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorVerifyQRCode", new Object[] { e.toString() }, locale));
    } catch (FormatException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorVerifyQRCode", new Object[] { e.toString() }, locale));
    } catch (NotFoundException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorVerifyQRCode", new Object[] { e.toString() }, locale));
    }
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) ChecksumException(com.google.zxing.ChecksumException) NotFoundException(com.google.zxing.NotFoundException) BitMatrix(com.google.zxing.common.BitMatrix) Decoder(com.google.zxing.qrcode.decoder.Decoder) BufferedImage(java.awt.image.BufferedImage) EncodeHintType(com.google.zxing.EncodeHintType) DecoderResult(com.google.zxing.common.DecoderResult) EnumMap(java.util.EnumMap) DecodeHintType(com.google.zxing.DecodeHintType) IOException(java.io.IOException) FormatException(com.google.zxing.FormatException) Graphics2D(java.awt.Graphics2D) AffineTransformOp(java.awt.image.AffineTransformOp) Detector(com.google.zxing.qrcode.detector.Detector) MultiFormatWriter(com.google.zxing.MultiFormatWriter) DetectorResult(com.google.zxing.common.DetectorResult) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) WriterException(com.google.zxing.WriterException)

Example 37 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project whole by wholeplatform.

the class ICNSGenerator method halfScale.

protected BufferedImage halfScale(BufferedImage image) {
    int w = image.getWidth() / 2;
    int h = image.getHeight() / 2;
    BufferedImage scaled = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    AffineTransform affineTransform = new AffineTransform();
    affineTransform.scale(.5, .5);
    AffineTransformOp operation = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BICUBIC);
    return operation.filter(image, scaled);
}
Also used : AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) AffineTransformOp(java.awt.image.AffineTransformOp)

Example 38 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project cytoscape-impl by cytoscape.

the class FontFaceIcon method downSample.

private void downSample(final BufferedImage bi, final Component c, final Graphics g, int x, int y) {
    int iw = bi.getWidth();
    int ih = bi.getHeight();
    double scale = Math.min((double) width / (double) iw, (double) height / (double) ih);
    final AffineTransform at = new AffineTransform();
    if (scale < 0)
        at.scale(scale, scale);
    final AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
    final Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    final Rectangle2D r2d = scaleOp.getBounds2D(bi);
    // scaled height
    double sh = r2d.getHeight();
    // scaled width
    double sw = r2d.getWidth();
    int vpad = (int) (y + (height - sh) / 2.0);
    int hpad = (int) (x + (width - sw) / 2.0);
    // draw it centered
    g2d.drawImage(bi, scaleOp, hpad, vpad);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) AffineTransformOp(java.awt.image.AffineTransformOp) Graphics2D(java.awt.Graphics2D)

Example 39 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project cytoscape-impl by cytoscape.

the class TextIcon method downSample.

private void downSample(final BufferedImage bi, final Component c, final Graphics g, int x, int y) {
    int iw = bi.getWidth();
    int ih = bi.getHeight();
    double scale = Math.min((double) width / (double) iw, (double) height / (double) ih);
    scale = scale > 1 ? 1 : scale;
    final AffineTransform at = new AffineTransform();
    at.scale(scale, scale);
    final AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
    final Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    final Rectangle2D r2d = scaleOp.getBounds2D(bi);
    // scaled height
    double sh = r2d.getHeight();
    // scaled width
    double sw = r2d.getWidth();
    int vpad = (int) (1 + y + (height - sh) / 2.0);
    int hpad = (int) (x + (width - sw) / 2.0);
    // draw it centered
    g2d.drawImage(bi, scaleOp, hpad, vpad);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) AffineTransformOp(java.awt.image.AffineTransformOp) Graphics2D(java.awt.Graphics2D)

Example 40 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project MtgDesktopCompanion by nicho92.

the class CardsPicPanel method mirroring.

private BufferedImage mirroring(BufferedImage image) {
    AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
    tx.translate(-image.getWidth(null), 0);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    image = op.filter(image, null);
    return image;
}
Also used : AffineTransform(java.awt.geom.AffineTransform) AffineTransformOp(java.awt.image.AffineTransformOp)

Aggregations

AffineTransformOp (java.awt.image.AffineTransformOp)48 AffineTransform (java.awt.geom.AffineTransform)31 BufferedImage (java.awt.image.BufferedImage)29 Graphics2D (java.awt.Graphics2D)20 IOException (java.io.IOException)4 Rectangle2D (java.awt.geom.Rectangle2D)3 WritableRaster (java.awt.image.WritableRaster)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Color (java.awt.Color)2 GradientPaint (java.awt.GradientPaint)2 Image (java.awt.Image)2 RenderingHints (java.awt.RenderingHints)2 ByteLookupTable (java.awt.image.ByteLookupTable)2 ConvolveOp (java.awt.image.ConvolveOp)2 LookupOp (java.awt.image.LookupOp)2 LookupTable (java.awt.image.LookupTable)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 ImageException (cbit.image.ImageException)1 GeometryException (cbit.vcell.geometry.GeometryException)1