import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.*;
public class image extends Frame
{
 public  static int w,h;  
 public static int ipixels[];
 Image Buf,img=null;
      
     public void initVal(){  
         String path="D:\\image\\79.jpg";
     img=this.getToolkit().getImage(path);   
         w=img.getWidth(null);
         h=img.getHeight(null);        
         ipixels=new int[w*h];            
         PixelGrabber pg = new PixelGrabber(img, 0, 0, w,h, ipixels, 0, w);//将RGB像素值存入ipixels
         
    }
    /*获取灰度值*/
    public static double[] getGrayValue(){
        double grayValue[] = new double[ipixels.length];
            for ( int i = 0; i < ipixels.length; i++ ){
                int r = (ipixels[i] >> 16)& 0xff;
                int g = (ipixels[i] >> 8 )& 0xff;
                int b = (ipixels[i]      )& 0xff;
                grayValue[i] =  0.2990 * r + 0.5870 * g + 0.1140 * b;
            }
            return grayValue;
    }
     public void showImage()
     {
      Image pic=createImage(new MemoryImageSource(w,h,grayValue,0,w));
        Buf.getGraphics().drawImage(pic,0,0,this); //显示黑白图片;
     }
      public  void showGrayArrayImg(int[] grayArray,int w,int h){
         for(int i=0;i<grayArray.length;i++) {
            grayArray[i]=(255<<24)|(grayArray[i]<<16)|(grayArray[i]<<8)|grayArray[i];
        }
        Image im= createImage((ImageProducer)new MemoryImageSource(w,h,grayArray,0,w));
        
    }
    
public static void main(String[] args) 
{
     image my=new image();
     my.initVal();
     my.getGrayValue();
     my.showImage();
}
}

解决方案 »

  1.   

    我很难相信你可以编译通过。你应该是学习java不久吧,说句实话,程序很乱,逻辑问题很多。
    以下是我自己写的,希望对你有所帮助:
    import java.awt.Image;
    import java.awt.image.MemoryImageSource;
    import java.awt.image.PixelGrabber;import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;public class GrayImage extends JFrame{
    //获取原始图片
    public  Image getOriImage(String path){
    Image image = new ImageIcon(path).getImage();
    return image;
    }

    //将图片image变灰,并返回变灰后的图片
    public  Image toGrayImage(Image image){
    Image grayImage=null;
    int w,h;
    int[] pixels;
    w=image.getWidth(null);
    h=image.getHeight(null);
    pixels=new int[w*h];
    getPixels(image,0,0,w,h,pixels);
    grayImage=createImage(new MemoryImageSource(w,h,pixels,0,w));


    return grayImage;
    }

    //将原始图片的像素变灰,并放入pixels

    public   void getPixels(Image image,int x,int y,int w,int h,int[] pixels){
    int gray;
    PixelGrabber pg=new PixelGrabber(image,x,y,w,h,pixels,0,w);
    try{
    pg.grabPixels();
    }catch(InterruptedException e){
    System.err.print("Interrupted waiting for pixels!");
    e.printStackTrace();
    return;
    }
    for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
    gray=(int)(((pixels[i*w+j]>>16)&0xff)*0.3);
    gray+=(int)(((pixels[i*w+j]>>8)&0xff)*0.59);
    gray+=(int)((pixels[i*w+j]&0xff)*0.11);
    pixels[i*w+j]=(255<<24)|(gray<<16)|(gray<<8)|gray;
    }
    }
    }

    public static void main(String[] args) {
    GrayImage gray = new GrayImage();
    Image OriImage = gray.getOriImage("2.jpg"); //改成你自己图片的路径
    Image grayImage = gray.toGrayImage(OriImage);
    JLabel label = new JLabel();
    label.setIcon(new ImageIcon(grayImage));
    gray.getContentPane().add(label);
    gray.pack();
    gray.setDefaultCloseOperation(EXIT_ON_CLOSE);
    gray.setVisible(true);
    }
    }
      

  2.   

    谢谢,如果要变成二值化图像是不是只要在,只像素变灰的for循环中判断pixels[i*w+j]是否大于阈值,大于pixels[i*w+j]=255,小于=0!
      

  3.   

    不应该用pixels[i*w+j]判断,应该用gray;
    如下:
    for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
    gray=(int)(((pixels[i*w+j]>>16)&0xff)*0.3);
    gray+=(int)(((pixels[i*w+j]>>8)&0xff)*0.59);
    gray+=(int)((pixels[i*w+j]&0xff)*0.11);

    if(gray>128) //改成你想要的阈值
    gray = 255;
    else
    gray = 0;

    pixels[i*w+j]=(255<<24)|(gray<<16)|(gray<<8)|gray;
    }
    }
      

  4.   

    真的非常感谢guofei_gf 的帮助。。再次谢谢你!!!