天天看点

java 图片 白边_java 去除图片白边 两种方法的比较

swt 下的图片处理:

ImageData ideaImageData = new ImageData(path);

ideaImageData = getWhite(ideaImageData);

ideaImageData.transparentPixel = ideaImageData.palette.getPixel(new RGB(255, 255, 255));

Image fullImage = new Image(container.getDisplay(), ideaImageData);

Label label = new Label(container, SWT.NONE);

label.setImage(fullImage);

//-----------------------------------------------------------------

效果不错

public ImageData getWhite(ImageData imageData) {

int threshold = 220;

RGB white = new RGB(255, 255, 255);

for (int i = 0; i < imageData.width; i++) {

for (int j = 0; j < imageData.height; j++) {

RGB rgb = imageData.palette.getRGB(imageData.getPixel(i, j));

if (rgb.red > threshold && rgb.green > threshold && rgb.blue > threshold)

imageData.setPixel(i, j, imageData.palette.getPixel(white));

}

}

return imageData;

}

效果比较差

public ImageData toPureWhite(ImageData imageData) {

int redShift = imageData.palette.redShift;

int greenShift = imageData.palette.greenShift;

int blueShift = imageData.palette.blueShift;

int[] lineData = new int[imageData.width];

int r, g, b, pixelValue;

for (int y = 0; y < imageData.height; y++) {

// Analyze each pixel value in the line

imageData.getPixels(0, y, imageData.width, lineData, 0);

for (int x = 0; x < lineData.length; x++) {

pixelValue = lineData[x];

r = pixelValue & redShift;

g = (pixelValue & greenShift) >> 8;

b = (pixelValue & blueShift) >> 16;

System.out.println("r=" + r + " g=" + g + " b=" + b);

if (r >= 230 && g > 230 && b > 150)

imageData.setPixel(x, y, 0xFFFFFF);

}

}

return imageData;

}