package com.eduask.luck.validate;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
/**
* 分割图片
*
* @author luck
*
*/
public class SplitImage {
public static void main(String[] args) throws Exception {
String file = "D:\\testimg/test2.jpg";
BufferedImage img = removeBackgroud(file);
splitImage(img);
}
public static void splitImage(BufferedImage img)
throws Exception {
List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
//subImgs.add(img.getSubimage(10, 10, 60, 20)); 数字对应 图片像素位置x轴,y轴,w宽,h高
//循环截取图片
int x = 12;//起始位置x坐标
int y = 7;//起始位置y坐标
int w = 83;//截取的小图片之间相隔宽度
int h = 23;//截取的小图片之间相隔高度
int xl = 101 ;//横向相隔距离
int yl = 49;//纵向相隔距离
for(int i =0 ; i< 10 ;i++){
for(int j = 0 ; j < 3 ; j ++){
subImgs.add(img.getSubimage(x+(xl*i), y+(yl*j), w, h));
}
}
int count = 0;
for (BufferedImage bufferedImage : subImgs) {
//图片路径 D:\testimg/tes
File file = new File("D:\\testimg/text/"+count+".jpg");
ImageIO.write(bufferedImage, "jpg", file);
count ++;
}
//return subImgs;
}
public static int isWhite(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() > 100) {
return 1;
}
return 0;
}
public static BufferedImage removeBackgroud(String picFile)
throws Exception {
BufferedImage img = ImageIO.read(new File(picFile));
int width = img.getWidth();
int height = img.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
if (isWhite(img.getRGB(x, y)) == 1) {
img.setRGB(x, y, Color.WHITE.getRGB());
} else {
img.setRGB(x, y, Color.BLACK.getRGB());
}
}
}
return img;
}
}
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
/**
* 分割图片
*
* @author luck
*
*/
public class SplitImage {
public static void main(String[] args) throws Exception {
String file = "D:\\testimg/test2.jpg";
BufferedImage img = removeBackgroud(file);
splitImage(img);
}
public static void splitImage(BufferedImage img)
throws Exception {
List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
//subImgs.add(img.getSubimage(10, 10, 60, 20)); 数字对应 图片像素位置x轴,y轴,w宽,h高
//循环截取图片
int x = 12;//起始位置x坐标
int y = 7;//起始位置y坐标
int w = 83;//截取的小图片之间相隔宽度
int h = 23;//截取的小图片之间相隔高度
int xl = 101 ;//横向相隔距离
int yl = 49;//纵向相隔距离
for(int i =0 ; i< 10 ;i++){
for(int j = 0 ; j < 3 ; j ++){
subImgs.add(img.getSubimage(x+(xl*i), y+(yl*j), w, h));
}
}
int count = 0;
for (BufferedImage bufferedImage : subImgs) {
//图片路径 D:\testimg/tes
File file = new File("D:\\testimg/text/"+count+".jpg");
ImageIO.write(bufferedImage, "jpg", file);
count ++;
}
//return subImgs;
}
public static int isWhite(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() > 100) {
return 1;
}
return 0;
}
public static BufferedImage removeBackgroud(String picFile)
throws Exception {
BufferedImage img = ImageIO.read(new File(picFile));
int width = img.getWidth();
int height = img.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
if (isWhite(img.getRGB(x, y)) == 1) {
img.setRGB(x, y, Color.WHITE.getRGB());
} else {
img.setRGB(x, y, Color.BLACK.getRGB());
}
}
}
return img;
}
}