首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >平移BufferedImage

平移BufferedImage
EN

Stack Overflow用户
提问于 2020-01-16 12:35:50
回答 1查看 26关注 0票数 0

我如何删除BufferedImage最左边垂直列的50px,并将其复制到与原始BufferedImage大小相同的新BufferedImage中?

代码语言:javascript
复制
class TestCopyImage {

    var img: BufferedImage? = null
    private val rnd = Random()

    fun create(screenWidth: Int, screenHeight: Int) {
        img = BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB)

        //Grab the graphics object off the image
        val graphics = img!!.createGraphics()

        //val stroke: Stroke = BasicStroke(1f)
        //graphics.setStroke(stroke);

        // Fill the image buffer
        for (i in 1..screenWidth) {
            for (j in 1..screenHeight) {
                val r: Int = rnd.nextInt(255)
                val g: Int = rnd.nextInt(255)
                val b: Int = rnd.nextInt(255)
                val randomColor = Color(r, g, b)
                graphics.paint = randomColor
                graphics.fill(Rectangle(i , j , 1, 1))
            }
        }

        // Get a subimage, deleting 50 pixels of the left-most vertical portion.
        img = img!!.getSubimage(50, 0, screenWidth - 50 , screenHeight)

        // TODO Now copy that into a new image, same size as the original buffer?
        img = BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB)
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-16 13:30:39

以下是您可以执行的操作的Java版本:

代码语言:javascript
复制
int panDist = 50;

BufferedImage subImg = img.getSubimage(panDist, 0, img.getWidth() - panDist, img.getHeight());

BufferedImage newImg = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
for (int x = 0; x < subImg.getWidth(); ++x) {
    for (int y = 0; y < subImg.getHeight(); ++y) {
        newImg.setRGB(x, y, subImg.getRGB(x, y));
    }
}

子图像实际上并不是必需的,您可以跳过它,只需执行以下操作:

代码语言:javascript
复制
int panDist = 50;

BufferedImage newImg = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
for (int x = panDist; x < img.getWidth(); ++x) {
    for (int y = 0; y < img.getHeight(); ++y) {
        newImg.setRGB(x - panDist, y, img.getRGB(x, y));
    }
}

您还可以稍微调整一下,以就地修改图像。

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

https://stackoverflow.com/questions/59763092

复制
相关文章

相似问题

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