楼上的基本正确,
问题一:
int[] rgb = new int[3];最好用二维数组
int[] rgb = new int[3][width*height]
问题二:
rgb[0] = (pixel & 0xff0000 ) >> 16 ;
rgb[1] = (pixel & 0xff00 ) >> 8 ;
rgb[2] = (pixel & 0xff );
会把数组内的值覆盖,获得就是最后像素点的RGB值;
我写了一个希望可以帮助到你
package imageReadAndWrite;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* JPG File reader/writer. Uses native com.sun libraries (which may deprecate at
* any time)
*
*
* @author PhoenixZJG
* @version 1.0
*/
public class JPGFile implements xxxFile {
private int[] ints = null;
private byte bytes[] = null; // bytes which make up binary PPM image
private double doubles[] = null;
private int[][] imageRGB = null;
private String filename = null; // filename for PPM image
private int height = 0;
private int width = 0;
/**
* Read the PPM File.
*
* @throws FileNotFoundException
* if the directory/image specified is wrong
* @throws IOException
* if there are problems reading the file.
*/
public JPGFile(String filename) throws FileNotFoundException, IOException {
this.filename = filename;
readImage();
}
/**
* Get the height of the PPM image.
*
* @return the height of the image.
*/
public int getHeight() {
return height;
}
/**
* Get the width of the PPM image.
*
* @return the width of the image.
*/
public int getWidth() {
return width;
}
/**
* Get the data as byte array. Data is of any type that has been read from
* the file (usually 8bit RGB)
*
* @return The data of the image.
*/
public byte[] getBytes() {
return bytes;
}
/**
* Get the data as double array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public double[] getDouble() {
return doubles;
}
/**
* Get the data as double array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public int[] getInt() {
return ints;
}
/**
* Get the data as integer array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public int[][] getImageRGB() {
return imageRGB;
}
/**
* Write to fn
file the data
using the
* width, height
variables. Data is assumed to be 8bit RGB.
*
* @throws FileNotFoundException
* if the directory/image specified is wrong
* @throws IOException
* if there are problems reading the file.
*/
public static void writeImage(String fn, int[] data, int width, int height)
throws FileNotFoundException, IOException {
FileOutputStream fOut = new FileOutputStream(fn);
JPEGImageEncoder jpeg_encode = JPEGCodec.createJPEGEncoder(fOut);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, data, 0, width);
jpeg_encode.encode(image);
fOut.close();
}
/**
* Read the image from the specified file.
*
* @throws FileNotFoundException
* pretty obvious
* @throws IOException
* filesystem related problems
*/
private void readImage() throws FileNotFoundException, IOException {
FileInputStream fIn = new FileInputStream(filename);
JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);
BufferedImage image = jpeg_decode.decodeAsBufferedImage();
width = image.getWidth();
height = image.getHeight();
int[] rgbdata = new int[width * height];
image.getRGB(0, 0, width, height, rgbdata, 0, width);
ints = rgbdata;
bytes = new byte[rgbdata.length];
doubles = new double[rgbdata.length];
imageRGB = new int[3][rgbdata.length];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (rgbdata[i] & 0xFF);
doubles[i] = (double) (rgbdata[i]);
imageRGB[0][i] = (rgbdata[i] & 16711680) >> 16;
imageRGB[1][i] = (rgbdata[i] & 65280) >> 8;
imageRGB[2][i] = (rgbdata[i] & 255);
}
}
}
上述代码可以复制,粘贴使用,有方法的注视,getImageRGB() 就可以获得所有像素的RGB值,你就可以在其他方法里处理这个二维数组,得到你想要的平均值了,处理后的值,记得把值逆运算,再转换到Int[]里,就可以用,writeImage()输出图像了。
//我在程序中打印出了每一个坐标的RGB值,你自己整理整理,求个平均值,
//放到你的那个二维数组里。
//自己用画图工具做一个小图片,注意图片的名字和程序中一致哦~
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
public class Test{
public static void main(String args[]) {
int[] rgb = new int[3];
File file = new File("a.bmp");
BufferedImage bi=null;
try{
bi = ImageIO.read(file);
}catch(Exception e){
e.printStackTrace();
}
int width=bi.getWidth();
int height=bi.getHeight();
int minx=bi.getMinX();
int miny=bi.getMinY();
System.out.println("width="+width+",height="+height+".");
System.out.println("minx="+minx+",miniy="+miny+".");
for(int i=minx;i
int pixel=bi.getRGB(i, j);
rgb[0] = (pixel & 0xff0000 ) >> 16 ;
rgb[1] = (pixel & 0xff00 ) >> 8 ;
rgb[2] = (pixel & 0xff );
System.out.println("i="+i+",j="+j+":("+rgb[0]+","+rgb[1]+","+rgb[2]+")");
}
}
}
}
主要是以下两点,按照思路你尝试一下
1,利用ImageIO.read 得到 BufferedImage
2,BufferedImage.getRGB()得到像素数组
然后后续的就是你自己处理了