publicfinalclassImageUtils{//publicImageUtils(){}publicstaticvoidreadDefinePixel(StringimagePath){//intggwidth=120;//intggheight=150;try{File_file=newFile(imagePath);...
public final class ImageUtils {
//public ImageUtils() {
//
//}
public static void readDefinePixel(String imagePath)
{
// int ggwidth=120;
// int ggheight=150;
try {
File _file = new File(imagePath);
BufferedImage src = ImageIO.read(_file);
//Image src = ImageIO.read(_file);
//图片的宽度
int wideth = src.getWidth(); //读取文件的长宽
//图片的高度
int height = src.getHeight();
System.out.println("图片宽度wideth:"+wideth);
System.out.println("图片高度height:"+height);
BufferedImage image = new BufferedImage(120, 150,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
//g.drawImage(src, 20, 20, wideth/2, height/2, null);
int x1=(wideth-120)/2;
int y1=(height-150)/2;
int x2=(wideth-120)/2+120;
int y2=(height-150)/2+150;
Color color = new Color(0x00,0x00,0x00);
g.drawImage(src, 0, 0, 120, 150, x1, y1, x2, y2, null);
//
//写文件
//取指定区域的图像
Color color1 = new Color(0,0,0);
g.setXORMode(color1);
g.dispose();
System.out.println(g.getColor());
//目标文件
File itemFile = new File("C:/app/mov/abc1.png");
FileOutputStream out = new FileOutputStream(itemFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
readDefinePixel("file/10.png");
}
}
展开