小弟遇到问题在取像素颜色时全部都是0//image已经事先得到
BufferedImage sourImage = new BufferedImage( image.getWidth( this), 
                                             image.getHeight(this),
                                             BufferedImage.TYPE_INT_ARGB);WritableRaster raster = sourceImage.getRaster();//用于装颜色数组
int []i = new int[ 4];//这两个参数11, 11是否是这个图像的第11行11列的那个像素?
raster.getPixel( 11, 11, i);Color c = new Color( i[ 0], i[ 1], i[ 2], i[ 3]);此时c打印全部为0,求各位指条明路!!还有一个问题就是设置颜色问题:int []black = { 0, 0, 0, 255};raster.setPixel( 1, 1, black);无反映,请指点!!

解决方案 »

  1.   

    你的问题我不太清楚,看这一段代码是看不出什么的,找个例子给你:import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.io.File;public class PictureReader { private PictureReader() {
    } public static WritableRaster makeRaster(File file, SIRDSLog sirdsLog) {

    JPanel jp = new JPanel(); // we first build a dummy raster, to return if something is wrong with the file.
    BufferedImage dummybi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
    Graphics2D dummyg2d = dummybi.createGraphics();
    WritableRaster raster = (WritableRaster) dummybi.getData();

    try {
    String fileName = file.getAbsolutePath();

    ImageIcon ii = new ImageIcon(fileName);
    int imgh = ii.getIconHeight();
    int imgw = ii.getIconWidth();
    Image img = ii.getImage();

    sirdsLog.log("Width : "+ imgw, 1);
    sirdsLog.log("Height : " + imgh, 1);


    BufferedImage bi = new BufferedImage(imgw, imgh, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(img, 0, 0, jp);

    raster = (WritableRaster) bi.getData();
    sirdsLog.log("PictureReader.makeRaster() done", 2);

    } catch (Exception ex) {
    sirdsLog.log("Ouch, something wrong with this file !", 1);
    ex.printStackTrace();
    } finally {
    return raster;
    }






    public static double [] [] buildZFromRaster(WritableRaster raster, SIRDSLog sirdsLog) {

    int[] pixelRGB = new int[3];
    int pixelGrey = 0;
    double z = 0;

    int rasterHeight = raster.getHeight();
    int rasterWidth = raster.getWidth();

    double [][] depthmap = new double [rasterHeight] [rasterWidth];

    for (int i = 0; i < rasterHeight; i++) {
    for (int j = 0; j < rasterWidth; j++) {
    pixelRGB = raster.getPixel(j, i, pixelRGB);
    pixelGrey = (int) Math.round((pixelRGB[0]+pixelRGB[1]+pixelRGB[2])/3.0);
    z = (pixelGrey/257.0);
    depthmap[i][j] = z;
    }
    }
    sirdsLog.log("PictureReader.buildZFromRaster() done", 2);
    return depthmap;

    }


    public static WritableRaster makeTileRaster(File file, int sirdsWidth, int sirdsHeight, SIRDSLog sirdsLog) {

    String fileName = file.getAbsolutePath();

    ImageIcon ii = new ImageIcon(fileName);
    int tileh = ii.getIconHeight();
    int tilew = ii.getIconWidth();
    Image tile = ii.getImage();

    sirdsLog.log("Tile width : "+ tilew, 1);
    sirdsLog.log("Tile height : " + tileh, 1);


    JPanel jp = new JPanel(); // building a new raster
    BufferedImage bi = new BufferedImage(sirdsWidth, sirdsHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(tile, 0, 0, jp);
    WritableRaster raster = (WritableRaster) bi.getData();

    int[] element = new int[3];
    int x;
    int y;
    for (int i = 0; i < sirdsHeight; i++) {
    for (int j = 0; j < sirdsWidth; j++) {
    x = j % tilew;
    y = i % tileh;
    element = raster.getPixel(x, y, element);
    raster.setPixel(j, i, element);
    }
    }

    sirdsLog.log("PictureReader.makeTileRaster() done", 2);
    return raster;

    }
    }