创建不同形状的像素?

时间:2018-07-17 17:33:42

标签: java image awt pixel pixels

我已经准备好通过维基百科了解Pixel,发现像素可以呈现为不同的形状,而不一定是正方形。This image shows alternative ways of reconstructing an image from a set of pixel values, using dots, lines, or smooth filtering.

如何在Java中创建具有不同形状像素的图像?

下面是我如何创建具有随机像素的图像

/**
 * File: RandomImage.java
 * 
 * Description:
 * Create a random color image.
 * 
 * @author King Amada
 * Date: 07-17-20148 tue
 */
 import java.io.File;
 import java.io.IOException;
 import java.awt.image.BufferedImage;
 import javax.imageio.ImageIO;
 public class RandomImage{
   public static void main(String args[])throws IOException{
      //image dimension
      int width = 640;
      int height = 320;
      //create buffered image object img
      BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
      //file object
      File f = null;
      //create random image pixel by pixel
      for(int y = 0; y < height; y++){
        for(int x = 0; x < width; x++){
          int a = (int)(Math.random()*256); //alpha
          int r = (int)(Math.random()*256); //red
          int g = (int)(Math.random()*256); //green
          int b = (int)(Math.random()*256); //blue

          int p = (a<<24) | (r<<16) | (g<<8) | b; //pixel

          img.setRGB(x, y, p);
        }
      }
      //write image
      try{
        f = new File("D:\\Image\\Output.png");
        ImageIO.write(img, "png", f);
      }catch(IOException e){
        System.out.println("Error: " + e);
      }
   }//main() ends here
 }//class ends here

0 个答案:

没有答案