如何将图片黑白化二

package image;
import acm.graphics.*;
import acm.program.*;
public class DisposeImage4ACM extends GraphicsProgram{
 public void run() {
  GImage image = new GImage("D:\\照片\\手机照片\\6.png");//get the image
  GImage gruyImage = createImage(image);//magnify the image for two
  
  image.scale(1.0);
  add(image,10,50);
  
  gruyImage.scale(1.0);
  add(gruyImage,10,50);
 }

 private GImage createImage(GImage image) {
  //Gets copy of Pixel from image
  int[][] array =  image.getPixelArray();
  
  int height = array.length; //gets heigh of image
  int width = array[0].length; //gets width of image
  
  for(int i=0; i<height; i++) {
   for(int j=0; j<width; j++) {
    int pixel = array[i][j];
    
    int r = GImage.getRed(pixel);
    int g = GImage.getGreen(pixel);
    int b = GImage.getBlue(pixel);
    int xx = colorForBW(r, g, b);
    array[i][j] = GImage.createRGBPixel(xx, xx, xx);
   }
  }
  return new GImage(array);
 }
 public int colorForBW(int r,int g,int b) {
  return GMath.round(r*0.299 + g*0.587 + b*0.144);
 }
}

你可能感兴趣的:(image,Class,手机,import,照片)