📄 image缩小.java
字号:
import javax.imageio.*;//ImageIO
import java.awt.image.BufferedImage;//BufferedImage
import java.io.*; //File IOException
public class Image缩小 {
public static void main(String[] args) throws Exception{
RgbHsv bo = new RgbHsv();
BufferedImage abc = bo.返回BufferedImage对象("D:/Backup/我的文档/My Pictures/power5.jpg");
//这两种格式都可以获得正确结果
int srcw = abc.getWidth();// 原图像的宽度 srcw = 1024
int srch = abc.getHeight(null);// 原图像的高度 srch = 768
BufferedImage uut = new BufferedImage(srcw / 2, srch / 2, 8) ;//依次构造生成的图像的宽度、高度、每一个RGB的位数
for(int i = 0; i < srcw -1; i += 2)
for(int j = 0; j < srch - 1; j += 2){
int rgb00 = abc.getRGB(i, j);
int rgb01 = abc.getRGB(i, j + 1);
int rgb10 = abc.getRGB(i + 1, j);
int rgb11 = abc.getRGB(i + 1, j + 1);
int r00 = (rgb00 & 0xff0000) >> 16;//获取r
int g00 = (rgb00 & 0x00ff00) >> 8;//获取b
int b00 = (rgb00 & 0x0000ff);//获取g
int r01 = (rgb01 & 0xff0000) >> 16;//获取r
int g01 = (rgb01 & 0x00ff00) >> 8;//获取b
int b01 = (rgb01 & 0x0000ff);//获取g
int r10 = (rgb10 & 0xff0000) >> 16;//获取r
int g10 = (rgb10 & 0x00ff00) >> 8;//获取b
int b10 = (rgb10 & 0x0000ff);//获取g
int r11 = (rgb11 & 0xff0000) >> 16;//获取r
int g11 = (rgb11 & 0x00ff00) >> 8;//获取b
int b11 = (rgb11 & 0x0000ff);//获取g
int r = (r00 + r01 + r10 + r11) / 4;
int g = (g00 + g01 + g10 + g11) / 4;
int b = (b00 + b01 + b10 + b11) / 4;
int rgb = (r << 16) + (g << 8) + b;
uut.setRGB(i / 2, j / 2, rgb);
}
ImageIO.write(uut, "jpg",new File("D:/Backup/我的文档/My Pictures/power5s图像缩小.jpg"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -