package org.ofbiz.common.qrcode;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.ofbiz.base.util.FileUtil;

/**
 * SCIPIO: qrcode util.
 */
public class QRCodeUtil {
    //private static final Debug.OfbizLogger module = Debug.getOfbizLogger(java.lang.invoke.MethodHandles.lookup().lookupClass());

    /**
     * Renders overlay over QRCode (original Scipio method).
     * @deprecated covered in generateQRCodeImage service
     */
    @Deprecated
    public static File drawLogoToFile(BufferedImage qrImage, String format, String logoPath) throws IOException {
        BufferedImage combined = drawLogo(qrImage, ImageIO.read(FileUtil.getFile(logoPath)));
        File imageFile = File.createTempFile("QRCode", "." + format);
        ImageIO.write(combined, "PNG", imageFile);
        imageFile.deleteOnExit();
        return imageFile;
    }

    /**
     * Renders overlay over QRCode (original Scipio method).
     * @deprecated covered in generateQRCodeImage service
     */
    @Deprecated
    public static BufferedImage drawLogo(BufferedImage qrImage, BufferedImage logoImage) {
        double determineImageScale = determineImageScale(logoImage.getWidth(), logoImage.getHeight(),
                qrImage.getWidth(), qrImage.getHeight());
        BufferedImage overlay = resizeImg(logoImage, determineImageScale);

        int deltaHeight = qrImage.getHeight() - overlay.getHeight();
        int deltaWidth  = qrImage.getWidth()  - overlay.getWidth();

        BufferedImage combined = new BufferedImage(qrImage.getWidth(),qrImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = combined.getGraphics();
        g.drawImage(qrImage, 0, 0, null);
        ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
        g.drawImage(overlay, Math.round(((float) deltaWidth) / 2), Math.round(((float) deltaHeight) / 2), null);
        return combined;
    }

    /**
     * Function to scale an image down, requires scaledWidth (original Scipio method).
     * @deprecated covered in generateQRCodeImage service; this does not use the system scalers currently.
     */
    @Deprecated
    public static BufferedImage resizeImg(BufferedImage image, double scaledWidth) {
        Image scaledImage = image.getScaledInstance((int) (image.getWidth() * scaledWidth), (int) (image.getHeight() * scaledWidth), Image.SCALE_SMOOTH);
        BufferedImage bufferedImage = new BufferedImage(
                scaledImage.getWidth(null),
                scaledImage.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        Graphics g = bufferedImage.createGraphics();
        g.drawImage(scaledImage, 0, 0, null);
        g.dispose();
        return bufferedImage;
    }

    /**
     * Function to determine proper aspect ratio (original Scipio method).
     * @deprecated covered in generateQRCodeImage service
     */
    @Deprecated
    private static double determineImageScale(int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {
        double scalex = (double) targetWidth / sourceWidth;
        double scaley = (double) targetHeight / sourceHeight;
        return Math.min(scalex, scaley);
    }
}

@@ -31,3338 +31,195 @@
 e;%0A%0A
-import java.awt.AlphaComposite;%0Aimport java.awt.Graphics;%0Aimport java.awt.Graphics2D;%0Aimport java.awt.Image;%0Aimport java.awt.image.BufferedImage;%0Aimport java.io.File;%0Aimport java.io.IOException;%0A%0Aimport javax.imageio.ImageIO;%0A%0Aimport org.ofbiz.base.util.FileUtil;%0A%0A/**%0A * SCIPIO: qrcode util.%0A */%0Apublic class QRCodeUtil %7B%0A    //private static final Debug.OfbizLogger module = Debug.getOfbizLogger(java.lang.invoke.MethodHandles.lookup().lookupClass());%0A%0A    /**%0A     * Renders overlay over QRCode (original Scipio method).%0A     * @deprecated covered in generateQRCodeImage service%0A     */%0A    @Deprecated%0A    public static File drawLogoToFile(BufferedImage qrImage, String format, String logoPath) throws IOException %7B%0A        BufferedImage combined = drawLogo(qrImage, ImageIO.read(FileUtil.getFile(logoPath)));%0A        File imageFile = File.createTempFile(%22QRCode%22, %22.%22 + format);%0A        ImageIO.write(combined, %22PNG%22, imageFile);%0A        imageFile.deleteOnExit();%0A        return imageFile;%0A    %7D%0A%0A    /**%0A     * Renders overlay over QRCode (original Scipio method).%0A     * @deprecated covered in generateQRCodeImage service%0A     */%0A    @Deprecated%0A    public static BufferedImage drawLogo(BufferedImage qrImage, BufferedImage logoImage) %7B%0A        double determineImageScale = determineImageScale(logoImage.getWidth(), logoImage.getHeight(),%0A                qrImage.getWidth(), qrImage.getHeight());%0A        BufferedImage overlay = resizeImg(logoImage, determineImageScale);%0A%0A        int deltaHeight = qrImage.getHeight() - overlay.getHeight();%0A        int deltaWidth  = qrImage.getWidth()  - overlay.getWidth();%0A%0A        BufferedImage combined = new BufferedImage(qrImage.getWidth(),qrImage.getHeight(), BufferedImage.TYPE_INT_ARGB);%0A        Graphics g = combined.getGraphics();%0A        g.drawImage(qrImage, 0, 0, null);%0A        ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));%0A        g.drawImage(overlay, Math.round(((float) deltaWidth) / 2), Math.round(((float) deltaHeight) / 2), null);%0A        return combined;%0A    %7D%0A%0A    /**%0A     * Function to scale an image down, requires scaledWidth (original Scipio method).%0A     * @deprecated covered in generateQRCodeImage service; this does not use the system scalers currently.%0A     */%0A    @Deprecated%0A    public static BufferedImage resizeImg(BufferedImage image, double scaledWidth) %7B%0A        Image scaledImage = image.getScaledInstance((int) (image.getWidth() * scaledWidth), (int) (image.getHeight() * scaledWidth), Image.SCALE_SMOOTH);%0A        BufferedImage bufferedImage = new BufferedImage(%0A                scaledImage.getWidth(null),%0A                scaledImage.getHeight(null),%0A                BufferedImage.TYPE_INT_RGB);%0A        Graphics g = bufferedImage.createGraphics();%0A        g.drawImage(scaledImage, 0, 0, null);%0A        g.dispose();%0A        return bufferedImage;%0A    %7D%0A%0A    /**%0A     * Function to determine proper aspect ratio (original Scipio method).%0A     * @deprecated covered in generateQRCodeImage service%0A     */%0A    @Deprecated%0A    private static double determineImageScale(int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) %7B%0A        double scalex = (double) targetWidth / sourceWidth;%0A        double scaley = (double) targetHeight / sourceHeight;%0A        return Math.min(scalex, scaley);%0A    %7D
+/**%0A * SCIPIO: qrcode util.%0A */%0Apublic class QRCodeUtil %7B%0A    //private static final Debug.OfbizLogger module = Debug.getOfbizLogger(java.lang.invoke.MethodHandles.lookup().lookupClass());
 %0A%7D%0A

remove all deprecated/unused methods (bugs?)