java – 如何水平翻转图像

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

解决方法:

我实际上是这样做的……

    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位置.干净,简单的代码:)

上一篇:httpclient源码分析之 PoolingHttpClientConnectionManager 获取连接 (转)


下一篇:C++多重继承分析——《虚继承实现原理(虚继承和虚函数)》