Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java为什么生成的图像总是翻转和旋转?_Java_Image Processing - Fatal编程技术网

Java为什么生成的图像总是翻转和旋转?

Java为什么生成的图像总是翻转和旋转?,java,image-processing,Java,Image Processing,我试图访问每个像素,对其进行操作,然后将其保存回系统。但最终的图像总是翻转和旋转,这是为什么? 以下是我的输入代码: BufferedImage input_image=ImageIO.read(new File("F:\\sophie4.png")); int result[][] = convertTo2DWithoutUsingGetRGB(input_image); private static int[][] convertTo2DWithoutUsingGetRGB(Buff

我试图访问每个像素,对其进行操作,然后将其保存回系统。但最终的图像总是翻转和旋转,这是为什么? 以下是我的输入代码:

 BufferedImage input_image=ImageIO.read(new File("F:\\sophie4.png"));
 int result[][] = convertTo2DWithoutUsingGetRGB(input_image);

 private static int[][] convertTo2DWithoutUsingGetRGB(BufferedImage image) {

      final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
      final int width = image.getWidth();
      final int height = image.getHeight();
      final boolean hasAlphaChannel = image.getAlphaRaster() != null;

      int[][] result = new int[height][width];
      if (hasAlphaChannel) {
         final int pixelLength = 4;
         for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
            argb += ((int) pixels[pixel + 1] & 0xff); // blue
            argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
            argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
            result[row][col] = argb;
            col++;
            if (col == width) {
               col = 0;
               row++;
            }
         }
      } else {
         final int pixelLength = 3;
         for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            argb += -16777216; // 255 alpha
            argb += ((int) pixels[pixel] & 0xff); // blue
            argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
            argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
            result[row][col] = argb;
            col++;
            if (col == width) {
               col = 0;
               row++;
            }
         }
      }

      return result;
   }
buffereImage输入_image=ImageIO.read(新文件(“F:\\sophie4.png”);
int result[][]=在不使用GetRGB(输入图像)的情况下转换为2d;
私有静态int[][]在不使用GetRGB(BuffereImage图像)的情况下转换为2d{
最终字节[]像素=((DataBufferByte)image.getRaster().getDataBuffer()).getData();
final int width=image.getWidth();
最终整数高度=image.getHeight();
最终布尔值hasAlphaChannel=image.getAlphaRaster()!=null;
int[][]结果=新的int[高度][宽度];
中频(信道){
最终整数像素长度=4;
对于(int-pixel=0,row=0,col=0;pixel
BufferedImage image = new BufferedImage(result.length, result[0].length, BufferedImage.TYPE_INT_RGB);
应该是

BufferedImage image = new BufferedImage(result[0].length, result.length, BufferedImage.TYPE_INT_RGB);

应该是

image.setRGB(col, row, result[row][col]); // See why that's consfusing

您正在使用
result[row][col]=argb;
处理图像,但使用
image.setRGB(x,y,result[x][y]写入);
,似乎backwards@MadProgrammer我已经用行和列替换了x&y。现在看代码,仍然不起作用。除非有什么我不明白的地方。如果我交换这两个,我会得到一个ArrayOutOfBoundException。
image.setRGB(row, col, result[row][col]);
image.setRGB(col, row, result[row][col]); // See why that's consfusing