1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/**
* Créer une image à partir d'une tableau de bytes.
* @param buffer
* @param width
* @param height
* @param nbBand
* @return
*/
public static BufferedImage createImagefromBuffer (
byte[] buffer,
int width,
int height,
int nbBand) {
try {
int type = (nbBand == 1)? BufferedImage.TYPE_BYTE_GRAY:
(nbBand == 3)? BufferedImage.TYPE_3BYTE_BGR:
(nbBand == 4)? BufferedImage.TYPE_4BYTE_ABGR:
BufferedImage.TYPE_CUSTOM;
BufferedImage image = new BufferedImage(width, height, type);
//Copie les données
DataBufferByte dbb = (DataBufferByte) image.getRaster().getDataBuffer();
System.arraycopy(buffer, 0, dbb.getData(), 0, width*height*nbBand);
return image;
} catch (ArrayIndexOutOfBoundsException boe) {
LOGGER.error("createImagefromBuffer", boe);
return null;
}
}
/**
* Miroir horizontal
* @param img
* @return
*/
public static BufferedImage horizontalflip(BufferedImage img) {
try {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage result = new BufferedImage(w, h, img.getType());
Graphics2D g = result.createGraphics();
setQuality(g);
g.drawImage(img, 0, 0, w, h, w, 0, 0, h, null);
return result;
} catch (Exception e) {
LOGGER.error("horizontalflip", e);
return null;
}
}
/**
* Miroir Vertical
* @param img
* @return
*/
public static BufferedImage verticalflip(BufferedImage img) {
try {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage result = new BufferedImage(w, h, img.getColorModel().getTransparency());
Graphics2D g = result.createGraphics();
setQuality(g);
g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null);
g.dispose();
return result;
} catch (Exception e) {
LOGGER.error("verticalflip", e);
return null;
}
}
/**
* Tourne l'image
* @param img
* @param angle
* @return
*/
public static BufferedImage rotate(BufferedImage img, int angle) {
try {
int w = img.getWidth();
int h = img.getHeight();
int dif = 0;
if(angle == 90 || angle == 270) {
dif = (w-h)/2;
w = img.getHeight();
h = img.getWidth();
}
BufferedImage result = new BufferedImage(w, h, img.getType());
Graphics2D g = result.createGraphics();
setQuality(g);
g.rotate(Math.toRadians(angle), w / 2, h / 2);
g.translate(-dif, dif);
g.drawImage(img, null, 0, 0);
return result;
} catch (Exception e) {
LOGGER.error("rotate "+angle, e);
return null;
}
}
/**
* Redimensionne l'image
* @param img
* @param newWidth
* @param newHeight
* @return
*/
public static BufferedImage resize(BufferedImage img, int newWidth, int newHeight) {
try {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(newWidth, newHeight, img.getType());
Graphics2D g = dimg.createGraphics();
setQuality(g);
g.drawImage(img, 0, 0, newWidth, newHeight, 0, 0, w, h, null);
g.dispose();
return dimg;
} catch (Exception e) {
LOGGER.error("resize["+newHeight+", "+newHeight+"]", e);
return null;
}
}
/**
* Copie l'image source dans l'image de destination
* @param src Image source, non nulle.
* @param dest Image de destination, si nulle créer l'image de retour.
* @return Copie de l'image source.
*/
public static BufferedImage copyImage(BufferedImage src, BufferedImage dest) {
try {
BufferedImage temp = (dest == null)? getBlanck(src): dest;
Graphics2D gd = temp.createGraphics();
gd.drawImage(src, null, 0, 0);
gd.dispose();
return temp;
} catch (Exception e) {
LOGGER.error("copyImage: src="+src+", dest="+dest, e);
return null;
}
} |
Partager