4

こんにちは私は水平方向に反転して画像を作成する方法を考えていました。実際のタスクでは、画像を読み取り、0〜5の明るさを示す画像に反転するコードが与えられました。画像を反転する必要がありました。

これは、画像を読んで描画する私のコードです。

public int[][] readImage(String url) throws IOException
{
    // fetch the image
    BufferedImage img = ImageIO.read(new URL(url));

    // create the array to match the dimensions of the image
    int width = img.getWidth();
    int height = img.getHeight();
    int[][] imageArray = new int[width][height];

    // convert the pixels of the image into brightness values
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            // get the pixel at (x,y) 

            int rgb = img.getRGB(x,y);
            Color c = new Color(rgb);
            int red = c.getRed();
            int green = c.getGreen();
            int blue = c.getBlue();

            // convert to greyscale
            float[] hsb = Color.RGBtoHSB(red, green, blue, null);                
            int brightness = (int)Math.round(hsb[2] * (PIXEL_CHARS.length - 1));

            imageArray[x][y] = brightness;
        }
    }
    return imageArray;
}

public void draw() throws IOException
{
    int[][] array = readImage("http://sfpl.org/images/graphics/chicklets/google-small.png");
    for(int i=0; i<array.length; i++)
    {
        for(int pic=0; pic<array[i].length; pic++)
        {
            if(array[pic][i] == 0)
            {
                System.out.print("X");
            }
            else if(array[pic][i] == 1)
            {
                System.out.print("8");
            }

            else if(array[pic][i] == 2)
            {
                System.out.print("0");
            }       

            else if(array[pic][i] == 3)
            {
                System.out.print(":");
            }

            else if(array[pic][i] == 4)
            {
                System.out.print(".");
            }

            else if (array[pic][i] == 5)
            {
                System.out.print(" ");
            }

            else 
            {
                System.out.print("error");
                break;
            }   

        }
        System.out.println();
    }
}    

これは、水平方向に反転するために作成しようとしたコードです。

void mirrorUpDown()
{
    int[][] array = readImage("http://sfpl.org/images/graphics/chicklets/google-small.png");
    int i = 0;

    for (int x = 0; x < array.length; x++)
    {
        for (int y = 0; y < array[i].length; y++)
        {{
                int temp = array[x][y]; 
                array[x][y]= array[-x][y]; 
                array[array[i].length-x][y]=temp;
            }
        }
    }

}    

エラーが発生します

 unreported exception java.io.IException;
 must be caught or declared to be thrown
4

4 に答える 4

3

私は実際にこの方法でそれを行うだろう...

    BufferedImage flip(BufferedImage sprite){
        BufferedImage img = new BufferedImage(sprite.getWidth(),sprite.getHeight(),BufferedImage.TYPE_INT_ARGB);
        for(int xx = sprite.getWidth()-1;xx>0;xx--){
            for(int yy = 0;yy < sprite.getHeight();yy++){
                img.setRGB(sprite.getWidth()-xx, yy, sprite.getRGB(xx, yy));
            }
        }
    return img;
}

xが最初の画像の終わりから始まり、そのrgba値を2番目の画像の反転位置に配置するループです。クリーンで簡単なコード:)

于 2017-09-04T18:38:00.863 に答える
1

関数 mirrorUpDown() 、そこに throws IOException を追加します。

また、これらのメソッドを呼び出している関数は、例外を処理するか、try catch ブロックで囲まれたコードを実行するか、関数も IOException をスローするように設定されます (いずれかが存在する必要があります)。

于 2012-09-24T10:15:10.107 に答える
1

imageArray からデータを取得する必要があることを、イメージはどのように認識しているのでしょうか。

代わりに、画像のラスターにアクセスして、その中のデータを変更する必要があります。

void flip(BufferedImage image) {
         WritableRaster raster = image.getRaster();
         int h = raster.getHeight();
         int w = raster.getWidth();
         int x0 = raster.getMinX();
         int y0 = raster.getMinY();
         for (int x = x0; x < x0 + w; x++){
             for (int y = y0; y < y0 + h / 2; y++){
                 int[] pix1 = new int[3];
                 pix1 = raster.getPixel(x, y, pix1);
                 int[] pix2 = new int[3];
                 pix2 = raster.getPixel(x, y0 + h - 1 - (y - y0), pix2);
                 raster.setPixel(x, y, pix2);
                 raster.setPixel(x, y0 + h - 1 - (y - y0), pix1);
             }
         }
         return;
    }
于 2012-09-24T10:39:55.177 に答える
1

これを1年以上後にここに投稿して申し訳ありませんが、ある段階で誰かを助けるはずです

   try{
   java.awt.image.BufferedImage bi = javax.imageio.ImageIO.read(getClass().getResource("Your image bro.jpg")) ;
 int[] h = bi.getRGB(0, 0, bi.getWidth(), bi.getHeight(), null, 0, bi.getWidth());
 int [] h1 = new int[h.length];
 System.out.println(""+h.length);
  for(int j = 0;500>j;j++){
   for(int i = 500;i>0;i--){
         h1[j*500+(500-i)] = h[(j*500)+(i-1)];
   }
  }
 bi.setRGB(0, 0, bi.getWidth(), bi.getHeight(), h1, 0, bi.getWidth());
      }
      catch(Exception e){e.printStackTrace();}

コードを分解しましょう

java.awt.image.BufferedImage bi =javax.imageio.ImageIO.read(getClass().getResource("Your image bro.jpg"));

画像の読み取りを試み、読み取った画像を BufferedImage 変数 bi に格納します

  int[] h = bi.getRGB(0, 0, bi.getWidth(), bi.getHeight(), null, 0, bi.getWidth());
  int [] h1 = new int[h.length];

2 つの配列をインスタンス化します。h は元の RGB 配列で、h1 は水平方向に反転された RGB 配列になります。

for(int j = 0;500>j;j++){
 for(int i = 500;i>0;i--){
  h1[j*500+(500-i)] = h[(j*500)+(i-1)];
 }
}

特定のものをもっと詳しく見てみましょう

  h1[j*500+(500-i)] = h[(j*500)+(i-1)];

画像は位置 0;0 から x.length;y.length までスキャンされますが、連続配列でスキャンされます。したがって、疑似配列を使用して画像の反転を操作します。j*500 は Y 値を参照し、(500-i) は x 値を参照します。

 bi.setRGB(0, 0, bi.getWidth(), bi.getHeight(), h1, 0, bi.getWidth());

最後に、画像が BufferedImage 変数に格納されます。

500 定数は、画像の x 解像度を参照していることに注意してください。たとえば、サイズが 1920 x 1080 の画像の場合、最大値は 1920 です。ロジックはユーザーが決定します。

于 2013-12-18T13:10:24.500 に答える