java图片处理效率_快要下班了,请教一下大佬们关于 Java 图像处理的问题

public static void main(String[] args) throws IOException {

BufferedImage bufferedImage

= ImageIO.read(new File(System.getProperty("user.dir" + "/test.jpg"));

BufferedImage grayImage =

new BufferedImage(bufferedImage.getWidth(),

bufferedImage.getHeight(),

bufferedImage.getType());

for (int i = 0; i < bufferedImage.getWidth(); i++) {

for (int j = 0; j < bufferedImage.getHeight(); j++) {

final int color = bufferedImage.getRGB(i, j);

final int r = (color >> 16) & 0xff;

final int g = (color >> 8) & 0xff;

final int b = color & 0xff;

int gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);;

System.out.println(i + " : " + j + " " + gray);

int newPixel = colorToRGB(255, gray, gray, gray);

grayImage.setRGB(i, j, newPixel);

}

}

File newFile = new File(System.getProperty("user.dir") + "/ok.jpg");

ImageIO.write(grayImage, "jpg", newFile);

}

如图,找 java 把图片转化成灰度图的时候找到的一段代码

疑问 1:

上面的位移操作,是不是因为 24 位的真彩图里面高到低位前 8 位是 R,中八位是 G,低 8 位是 B,这样直接位移是不是高位补 0,所以这个操作就是直接算 RGB 分别的值?

疑问 2:

一般图片都是 24 位的吗?那遇到 32 位的,前三个 8 位是 RGB,最后一个 8 位是亮度?( google 来的,不确定,因为没提到排列顺序)。另外是不是应该读取图片的位深再根据位深做不同的处理?

疑问 3:

那个 & 0xff 的效果我知道是会把负数变成正数,因为作为没有无符号 int,所以要这样与一下,把第一位正负号消掉,我想问的是这样真的不会有影响吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值