
z11.I_mage Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package z11;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Mode;
/**
*
* @author vietduc
*/
public class I_mage {
public static BufferedImage readImage(String filePath) throws IOException {
return ImageIO.read(new File(filePath));
}
public static BufferedImage readImage(InputStream ins) throws IOException {
return ImageIO.read(ins);
}
public static void writeImage(BufferedImage img, String filePath, String imageFormat) throws IOException {
ImageIO.write(img, imageFormat, new File(filePath));
}
public static void writeImage(BufferedImage img, String filePath) throws IOException {
ImageIO.write(img, "JPG", new File(filePath));
}
public static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
return Scalr.resize(image, Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS);
}
public static BufferedImage getScaledSmallImage(BufferedImage image, int width, int height) throws IOException {
if (image.getWidth() <= width && image.getHeight() <= height) {
return image;
}
return getScaledImage(image, width, height);
}
public static BufferedImage cropImage(BufferedImage src, int width, int height) {
BufferedImage dest = src.getSubimage(0, 0, width, height);
return dest;
}
public static BufferedImage cropSquareType1(BufferedImage src) {
try {
int width = src.getWidth();
int height = src.getHeight();
if (width < height) {
return src.getSubimage(0, (height - width) / 2, width, width);
} else if (width > height) {
return src.getSubimage((width - height) / 2, 0, height, height);
} else {
return src;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static boolean base64ToImage(String base64, String filePath) {
try {
byte [] dearr = Base64.decodeBase64(base64);
File file = new File(filePath);
FileUtils.writeByteArrayToFile(file, dearr);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static BufferedImage base64ToImage(String base64) throws IOException {
byte[] bytes = Base64.decodeBase64(base64);
return bytesToImage(bytes);
}
public static BufferedImage bytesToImage(byte [] bytes) throws IOException {
return ImageIO.read(new ByteArrayInputStream(bytes));
}
public static BufferedImage makeGrayScaleImage(BufferedImage colorImage) {
BufferedImage image = new BufferedImage(colorImage.getWidth(), colorImage.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(colorImage, 0, 0, null);
g.dispose();
return image;
}
public static void main(String[] args) throws IOException {
/* test scale image
BufferedImage img1 = ImageIO.read(new File("C:\\Users\\vietduc\\Desktop\\g1.jpg"));
BufferedImage img2 = I_mage.getScaledImage(img1, 300, 200);
File outputfile = new File("C:\\Users\\vietduc\\Desktop\\g2.jpg");
ImageIO.write(img2, "JPG", outputfile);
*/
String image_path = "/Users/danny/Desktop/av_nam";
String image_out = "/Users/danny/workspace/nb/http_service/toeic_campaign_api/img_avata_2";
String[] files = F_ile.listFileInfolder(image_path);
for (int i = 0; i < files.length; i++) {
System.out.println("img: " + i + " - " + files[i]);
BufferedImage img_in_i = ImageIO.read(new File(image_path + "/" + files[i]));
BufferedImage img_out_i = cropSquareType1(img_in_i);
if (img_out_i != null) {
ImageIO.write(img_out_i, "JPG", new File(image_out + "/avata_n_" + i + ".jpg"));
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy