java - 如何水平翻转图像

标签 java

您好,我想知道如何水平翻转和成像,在练习任务中,我得到了一个读取图像的代码,将其反转为表示亮度为 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 值放在第二张图像的翻转位置。干净、简单的代码:)

关于java - 如何水平翻转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12562680/

相关文章:

java - 如何在 SSL 握手中的 JKS 中获取正确的证书

java - GWT RPC 无法正常工作

java父级访问子级静态变量

java - android更改选择器和选项卡下划线的默认颜色

java - 对于某些任务,Gradle 执行会卡住 3 分钟

java - 在不初始化类的情况下测试方法

java - 从 .db 文件中一次读取一行文本

java - PropertyEditor不是用于类型转换而是用于字符串操作

java - 如何在 Spring Boot 中限制仅 5 个设备的登录?

java - 从 ArrayList 中删除所有特定值