Commit 30d83e03 authored by Vincent Calame's avatar Vincent Calame

Changement dans la bibliothèque des utilitaires

parent bdb1bc04
/* UtilLib_Image - Copyright (c) 2010-2018 Vincent Calame - Exemole
* Logiciel libre donné sous triple licence :
* 1) selon les termes de la CeCILL V2
* 2) selon les termes de l’EUPL V.1.1
* 3) selon les termes de la GNU GPLv3
* Voir le fichier licences.txt
*/
package net.mapeadores.util.images;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import net.mapeadores.util.awt.ResizeInfo;
/**
*
* @author Vincent Calame
*/
public final class ImageResizing {
private ImageResizing() {
}
public static void resize(File srcFile, File destFile, ResizeInfo resizeInfo, String format) throws IOException {
BufferedImage srcImg = ImageIO.read(srcFile);
BufferedImage destImg = getScaledInstance(srcImg, resizeInfo);
ImageIO.write(destImg, format, destFile);
}
public static void resize(InputStream inputStream, OutputStream outputStream, ResizeInfo resizeInfo, String format) throws IOException {
BufferedImage srcImg = ImageIO.read(inputStream);
BufferedImage destImg = getScaledInstance(srcImg, resizeInfo);
ImageIO.write(destImg, format, outputStream);
}
public static void resize(InputStream inputStream, File destFile, ResizeInfo resizeInfo, String format) throws IOException {
BufferedImage srcImg = ImageIO.read(inputStream);
BufferedImage destImg = getScaledInstance(srcImg, resizeInfo);
ImageIO.write(destImg, format, destFile);
}
public static BufferedImage getScaledInstance(BufferedImage img, ResizeInfo resizeInfo) {
int width = img.getWidth();
int height = img.getHeight();
short type = resizeInfo.getType();
int targetWidth = resizeInfo.getWidth();
int targetHeight = resizeInfo.getHeight();
if (resizeInfo.isOnlyMax()) {
switch (type) {
case ResizeInfo.WIDTH_RESIZING:
if (width <= targetWidth) {
return img;
}
break;
case ResizeInfo.HEIGHT_RESIZING:
if (height <= targetHeight) {
return img;
}
break;
case ResizeInfo.BOTH_RESIZING:
if ((width <= targetWidth) && (height <= targetHeight)) {
return img;
}
break;
}
}
float ratio = 0;
switch (type) {
case ResizeInfo.WIDTH_RESIZING:
if (targetWidth == width) {
return img;
}
ratio = ((float) targetWidth) / width;
targetHeight = (int) (ratio * height);
break;
case ResizeInfo.HEIGHT_RESIZING:
if (targetHeight == height) {
return img;
}
ratio = ((float) targetHeight) / height;
targetWidth = (int) (ratio * width);
break;
case ResizeInfo.BOTH_RESIZING:
if (resizeInfo.isOnlyMax()) {
float widthRatio = ((float) targetWidth) / width;
float heightRatio = ((float) targetHeight) / height;
if (widthRatio < heightRatio) {
ratio = widthRatio;
targetHeight = (int) (ratio * height);
} else {
ratio = heightRatio;
targetWidth = (int) (ratio * width);
}
} else {
ratio = Math.max(((float) targetWidth) / width, ((float) targetHeight) / height);
}
break;
}
if (ratio > 1) {
return getScaledInstance(img, targetWidth, targetHeight, RenderingHints.VALUE_INTERPOLATION_BICUBIC, false);
} else {
return getScaledInstance(img, targetWidth, targetHeight, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
}
}
/**
*
* Convenience method that returns a scaled instance of the provided
* {@code BufferedImage}. From
* <a href="http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html">The
* Perils of Image.getScaledInstance()</a>
* by Chris Campbell
*
* @param img the original image to be scaled
* @param targetWidth the desired width of the scaled instance, in pixels
* @param targetHeight the desired height of the scaled instance, in pixels
* @param hint one of the rendering hints that corresponds to
* {@code RenderingHints.KEY_INTERPOLATION} (e.g.
* {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
* {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
* {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
* @param higherQuality if true, this method will use a multi-step scaling
* technique that provides higher quality than the usual one-step technique
* (only useful in downscaling cases, where {@code targetWidth} or
* {@code targetHeight} is smaller than the original dimensions, and
* generally only when the {@code BILINEAR} hint is specified)
* @return a scaled version of the original {@code BufferedImage}
*/
public static BufferedImage getScaledInstance(BufferedImage img,
int targetWidth,
int targetHeight,
Object hint,
boolean higherQuality) {
int type = (img.getTransparency() == Transparency.OPAQUE)
? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = (BufferedImage) img;
int w, h;
if (higherQuality) {
// Use multi-step technique: start with original size, then
// scale down in multiple passes with drawImage()
// until the target size is reached
w = img.getWidth();
h = img.getHeight();
} else {
// Use one-step technique: scale directly from original
// size to target size with a single drawImage() call
w = targetWidth;
h = targetHeight;
}
do {
if (higherQuality && w > targetWidth) {
w /= 2;
if (w < targetWidth) {
w = targetWidth;
}
}
if (higherQuality && h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
}
BufferedImage tmp = new BufferedImage(w, h, type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();
ret = tmp;
} while (w != targetWidth || h != targetHeight);
return ret;
}
}
/* UtilLib - Copyright (c) 2010-2018 Vincent Calame - Exemole
* Logiciel libre donné sous triple licence :
* 1) selon les termes de la CeCILL V2
* 2) selon les termes de l’EUPL V.1.1
* 3) selon les termes de la GNU GPLv3
* Voir le fichier licences.txt
*/
package net.mapeadores.util.images;
import java.awt.Graphics;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
*
* @author Vincent Calame
*/
public final class ImagesUtils {
private ImagesUtils() {
}
public static void convert(InputStream input, String destinationFormatName, File destination) throws IOException {
BufferedImage bufferedImage = ImageIO.read(input);
ImageIO.write(bufferedImage, destinationFormatName, destination);
}
public static void cropImage(InputStream inputStream, String format, OutputStream outputStream, int x, int y, int width, int height) throws IOException {
BufferedImage srcImg = ImageIO.read(inputStream);
int type = (srcImg.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage destImg = new BufferedImage(width, height, type);
Graphics g = destImg.getGraphics();
g.drawImage(srcImg, 0, 0, width, height, x, y, x + width, y + height, null);
g.dispose();
ImageIO.write(destImg, format, outputStream);
}
public static boolean testImage(File file) {
try (FileInputStream is = new FileInputStream(file)) {
return testImage(is);
} catch (IOException ioe) {
return false;
}
}
public static boolean testImage(InputStream inputStream) {
try {
BufferedImage srcImg = ImageIO.read(inputStream);
return true;
} catch (IOException ioe) {
return false;
}
}
}
/* UtilLib - Copyright (c) 2018 Vincent Calame - Exemole
* Logiciel libre donné sous triple licence :
* 1) selon les termes de la CeCILL V2
* 2) selon les termes de l’EUPL V.1.1
* 3) selon les termes de la GNU GPLv3
* Voir le fichier licences.txt
*/
package net.mapeadores.util.json;
/**
*
* @author Vincent Calame
*/
public final class JsonUtils {
public final static PropertyEligibility ALL_ELIGIBILITY = new BooleanPropertyEligibility(true);
public final static PropertyEligibility NONE_ELIGIBILITY = new BooleanPropertyEligibility(false);
private JsonUtils() {
}
private static class BooleanPropertyEligibility implements PropertyEligibility {
private final boolean bool;
private BooleanPropertyEligibility(boolean bool) {
this.bool = bool;
}
@Override
public boolean includeProperty(String propertyName) {
return bool;
}
}
}
......@@ -24,30 +24,33 @@ public final class ExtendedCurrency implements Comparable<ExtendedCurrency> {
private final static ConcurrentMap<String, ExtendedCurrency> internMap = new ConcurrentHashMap<String, ExtendedCurrency>();
private final String code;
private final String symbol;
private final int fractionDigits;
private final Currency currency;
private final boolean symbolBefore;
static {
init("€", "EUR");
init("$", "USD");
init("¥", "JPY");
init("£", "GBP");
init("CFA", 0, false);
init("€", "EUR", true);
init("$", "USD", false);
init("¥", "JPY", false);
init("£", "GBP", false);
init("CFA", 0, false, null);
}
private ExtendedCurrency(String code, int fractionDigits, boolean symbolBefore) {
private ExtendedCurrency(String code, int fractionDigits, boolean symbolBefore, String symbol) {
this.code = code;
this.fractionDigits = fractionDigits;
this.currency = null;
this.symbolBefore = symbolBefore;
this.symbol = symbol;
}
private ExtendedCurrency(Currency currency) {
private ExtendedCurrency(Currency currency, String symbol) {
this.code = currency.getCurrencyCode();
this.fractionDigits = currency.getDefaultFractionDigits();
this.currency = currency;
this.symbolBefore = isSymbolBefore(currency.getCurrencyCode());
this.symbol = symbol;
}
public String getCurrencyCode() {
......@@ -64,7 +67,9 @@ public final class ExtendedCurrency implements Comparable<ExtendedCurrency> {
}
public String getSymbol() {
if (currency != null) {
if (symbol != null) {
return symbol;
} else if (currency != null) {
return currency.getSymbol();
} else {
return code;
......@@ -112,19 +117,24 @@ public final class ExtendedCurrency implements Comparable<ExtendedCurrency> {
}
try {
Currency currency = Currency.getInstance(currencyCode);
instance = new ExtendedCurrency(currency);
instance = new ExtendedCurrency(currency, null);
} catch (IllegalArgumentException iae) {
checkCode(currencyCode);
instance = new ExtendedCurrency(currencyCode, 2, false);
instance = new ExtendedCurrency(currencyCode, 2, false, null);
}
ExtendedCurrency previous = internMap.putIfAbsent(currencyCode, instance);
return (previous != null ? previous : instance);
}
private static void init(String symbol, String code) {
private static void init(String symbol, String code, boolean symbolFix) {
try {
Currency currency = Currency.getInstance(code);
ExtendedCurrency instance = new ExtendedCurrency(currency);
ExtendedCurrency instance;
if (symbolFix) {
instance = new ExtendedCurrency(currency, symbol);
} else {
instance = new ExtendedCurrency(currency, null);
}
internMap.putIfAbsent(code, instance);
internMap.putIfAbsent(symbol, instance);
} catch (IllegalArgumentException iae) {
......@@ -145,8 +155,8 @@ public final class ExtendedCurrency implements Comparable<ExtendedCurrency> {
}
}
private static void init(String code, int fractionDigits, boolean symbolBefore) {
ExtendedCurrency instance = new ExtendedCurrency(code, fractionDigits, symbolBefore);
private static void init(String code, int fractionDigits, boolean symbolBefore, String symbol) {
ExtendedCurrency instance = new ExtendedCurrency(code, fractionDigits, symbolBefore, symbol);
internMap.putIfAbsent(code, instance);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment