如何水平翻转图像

时间:2012-09-24 09:58:28

标签: java invert

HiI想知道如何水平翻转和图像,为了实际的任务我给了一个读取图像的代码,将其反转为一个图像,指示它的亮度从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 个答案:

答案 0 :(得分: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值放在第二个图像的fliped位置。干净,简单的代码:)

答案 1 :(得分:1)

函数mirrorUpDown(),在那里添加抛出IOException。

此外,您正在调用这些方法的函数是否处理异常,是否包含在try catch块中的代码或函数也设置为抛出IOException(其中一个应该在那里)

答案 2 :(得分: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;
    }

答案 3 :(得分: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];

实例化两个数组,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 但它是在一个coninual阵列中扫描。因此,我们使用伪数组来操纵图像的翻转。 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.逻辑由您自己决定。