给定一幅图像,用java读取每个像素的RGB三个颜色值

给定一幅图像,用java读取每个像素的RGB三个颜色值,第1张

int rgbR;

int rgbG;

int rgbB;

int minx = 0;

int miny = 0;

try {

File file = new File("E:\\ddpng");

BufferedImage image = ImageIOread(file);

int width = imagegetWidth();//宽度

int height = imagegetHeight();//高度

for (int i = minx; i < width; i++) {

for (int j = miny; j < height; j++) {

int pixel = imagegetRGB(i, j); // 下面三行代码将一个数字转换为RGB数字

rgbR = (pixel & 0xff0000) >> 16;

rgbG = (pixel & 0xff00) >> 8;

rgbB = (pixel & 0xff);

Systemoutprintln("i=" + i + ",j=" + j + ":(" + rgbR + "," + rgbG + "," + rgbB + ")");

}

}

Systemoutprintln("宽度为:"+width+",高度为:"+height);

} catch (IOException e) {

Systemoutprintln("读取文件出错");

eprintStackTrace();

}

占内存容量计算公式为:

所占内存大小 = 长度(像素) 宽度(像素) 一个像素所占内存空间(单位:字节

一般地,一个象素所占内存空间根据机器颜色数(专业词汇叫色深Color Depth)来决定:(1)Nokia 老S40机器 颜色数为4096色,就是2^12, 一个象素所占内存空间为15个字节;(2)Nokia S40新版 机器和 S60机器 颜色数为65536色,就是2^16, 一个象素所占内存空间为2个字节;有些机器颜色数为26w色,就是2^18=22字节,1600w = 2^24=3个字节,一个象素所占内存空间为4个字节。

ps:专业名词“8位”是指所能表现的颜色深度:一个8位图像仅最多只能支持256(2^8)种不同颜色,1个字节。

也就是压缩,可以不修改任何大小的压缩(速度快),也可等比例修改大小压缩(较慢)

下面这是一段等比例缩小的方法。

public String compressPic(String inputDir, String outputDir,

String inputFileName, String outputFileName, int width,

int height, boolean gp,String hzm) {

try {

if (!imageexists()) {

return "";

}

Image img = ImageIOread(image);

// 判断格式是否正确

if (imggetWidth(null) == -1) {

return "no";

} else {

int newWidth; int newHeight;

// 判断是否是等比缩放

if (gp == true) {

// 为等比缩放计算输出的宽度及高度

double rate1 = ((double) imggetWidth(null)) / (double) width ;

double rate2 = ((double) imggetHeight(null)) / (double) height ;

// 根据缩放比率大的进行缩放控制

double rate = rate1 > rate2 rate1 : rate2;

newWidth = (int) (((double) imggetWidth(null)) / rate);

newHeight = (int) (((double) imggetHeight(null)) / rate);

} else {

newWidth = imggetWidth(null); // 输出的宽度

newHeight = imggetHeight(null); // 输出的高度

}

BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImageTYPE_INT_RGB);

/

ImageSCALE_SMOOTH 的缩略算法 生成缩略的平滑度的

优先级比速度高 生成的质量比较好 但速度慢

/

Image im = imggetScaledInstance(newWidth, newHeight, ImageSCALE_SMOOTH);

taggetGraphics()drawImage(im, 0, 0, null);

FileOutputStream out = new FileOutputStream(outputDir + outputFileName);

//JPEGImageEncoder可适用于其他类型的转换

JPEGImageEncoder encoder = JPEGCodeccreateJPEGEncoder(out);

encoderencode(tag);

outclose();

}

} catch (IOException ex) {

exprintStackTrace();

}

return "ok";

}

代码不完整,测试不了。

/

旋转为指定角度

@param bufferedimage

目标图像

@param degree

旋转角度

@return

/

public static BufferedImage rotateImage(final BufferedImage bufferedimage, final int degree){

int w= bufferedimagegetWidth();// 得到宽度。

int h= bufferedimagegetHeight();// 得到高度。

int type= bufferedimagegetColorModel()getTransparency();// 得到透明度。

BufferedImage img;// 空的。

Graphics2D graphics2d;// 空的画笔。

(graphics2d= (img= new BufferedImage(w, h, type))createGraphics())setRenderingHint( RenderingHintsKEY_INTERPOLATION, RenderingHintsVALUE_INTERPOLATION_BILINEAR);

graphics2drotate(MathtoRadians(degree), w / 2, h / 2);// 旋转,degree是整型,度数,比如垂直90度。

graphics2ddrawImage(bufferedimage, 0, 0, null);// 从bufferedimagecopy至img,0,0是img的坐标。

graphics2ddispose();

return img;// 返回复制好的,原依然没有变,没有旋转,下次还可以使用。

}

看看别人的写法

以上就是关于给定一幅图像,用java读取每个像素的RGB三个颜色值全部的内容,包括:给定一幅图像,用java读取每个像素的RGB三个颜色值、java哪个类提供了获取图片所占存储空间大小或者如何计算求详细、java如何实现把一个大图片压缩到指定大小的图片且长宽比不变等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9318931.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存