首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在Java中反转存储在数组中的图像

尝试在Java中反转存储在数组中的图像
EN

Stack Overflow用户
提问于 2013-03-28 02:57:13
回答 1查看 1.2K关注 0票数 1

我需要反转存储在double[] img中的给定长度和宽度的图像;这是我第一次使用数组。指令将嵌套for循环、y(行)上的外循环和x(列)上的内循环,并反转每个水平数组。这就是我所拥有的,但它不工作。

代码语言:javascript
复制
width = ImageLibrary.getImageWidth();
height = ImageLibrary.getImageHeight();

  for(i = 0; i < width ; i++){
    for(j = 0; j < height ; j++){
       for(int k = 0; k < img.length/2; k++){
           double temp = img[k];
           img[i] = img[img.length - k - 1];
           img[img.length - k - 1] = temp;
}
    }
  }  

我真的不知道该怎么办?当它说要反转水平数组时,我做得对吗?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-28 03:06:21

我觉得你要找的更像这样

代码语言:javascript
复制
width = ImageLibrary.getImageWidth();
height = ImageLibrary.getImageHeight();

// Loop from the top of the image to the bottom
for (y = 0; y < height ; y++) {

    // Loop halfway across each row because going all the way will result
    // in all the numbers being put back where they were to start with
    for (x = 0; x < width / 2 ; x++) {

        // Here, `y * width` gets the row, and `+ x` gets position in that row
        double temp = img[y * width + x];

        // Here, `width - x - 1` gets x positions in from the end of the row
        // Subtracting 1 because of 0-based index
        img[y * width + x] = img[y * width + (width - x - 1)];
        img[y * width + (width - x - 1)] = temp;
    }
}

这将镜像图像,因此现在左侧是右侧,右侧是左侧

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15667170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档