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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值