Панорамирование BufferedImage - PullRequest
       1

Панорамирование BufferedImage

0 голосов
/ 16 января 2020

Как я могу удалить скажем 50px самого левого вертикального столбца BufferedImage и скопировать его в новый BufferedImage того же размера, что и оригинальный BufferedImage?

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)
    }
}

1 Ответ

1 голос
/ 16 января 2020

Вот Java версия того, что вы можете сделать:

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));
    }
}

Субизображение на самом деле не нужно, но вы можете пропустить это и просто сделать это вместо этого:

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));
    }
}

Вы также можете слегка изменить его, чтобы вместо этого изменить изображение на месте.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...