拆分和重新加入一个位图 [英] Splitting and rejoining a bitmap

查看:137
本文介绍了拆分和重新加入一个位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序,处理大型位图,并需要将位图分割成单独的砖,并在最后的位图拼接的他们重新走到一起之前单独处理每个tile。

I am writing an Android app that processes large bitmaps, and need to split the bitmap into separate 'tiles' and process each tile individually before stiching them back together in the final bitmap.

这是如何做到这一点任何线索?我认为这将是足够简单,使用createBitmap(),并指定在几个嵌套的for循环的更小的砖,但它并不像我想的那么简单,因为我想象中的setPixels不工作会。

Any clues on how to do this? I thought it would be simple enough using createBitmap() and specifying the smaller tiles in a couple of nested for loops, but it's not as easy as I thought, because setPixels does not work as I thought it would.

一个并发症我是砖需要重叠它们不是在大位图的边缘,其中,由于该处理需要看到一对额外的像素的周围的位图的两侧。我解决这个,如果没有必要通过简单地增加一对左右图像的边缘黑象素的层到图像分割,但因为他们需要的实际周围像素的信息,这将不会对瓷砖工作或处理将无法正常工作。

A complication I have is that the 'tiles' need to overlap where they are not at the edge of the bigger bitmap, since the processing needs to see a couple of extra pixels around the sides of the bitmaps. I get around this if there is no need to split the image by simply adding a couple of layers of black pixels around the edges of the image, but this won't work for the tiles as they need the information of the actual surrounding pixels or the processing will not work.

是否有更简单的方法来这样做呢?如果没有,我怎么去使用的setPixels并做createBitmap?

Are there any easier methods to doing this? If not, how do I go about doing it using setPixels and createBitmap?

我的code迄今:

        Bitmap finalImg = Bitmap.createBitmap(sourceImage.getWidth(), sourceImage.getHeight(), Bitmap.Config.ARGB_8888);  //Bitmap to store the final, processed image
        Bitmap tile = null;  //Temporary Bitmap to store tiles

        int tileDiameter = 500;  //Width and height of tiles
        int borderWidth = 5;  //Amount of pixel overlap from other tiles

        for (int y = 0 ; y < sourceImage.getHeight() ; y += tileDiameter) {
            for (int x = 0 ; x < sourceImage.getWidth() ; x += tileDiameter) {
                if (x == 0) {
                    if (y == 0) {
                        tile = Bitmap.createBitmap(sourceImage, x, y, (tileDiameter + borderWidth), (tileDiameter + borderWidth));
                    }
                    else {
                        tile = Bitmap.createBitmap(sourceImage, x, (y - borderWidth), (tileDiameter + borderWidth), (tileDiameter + borderWidth));
                    }
                }
                else {
                    if (y == 0) {
                        tile = Bitmap.createBitmap(sourceImage, (x - borderWidth), y, (tileDiameter + borderWidth), (tileDiameter + borderWidth));
                    }
                    else {
                        tile = Bitmap.createBitmap(sourceImage, (x - borderWidth), (y - borderWidth), (tileDiameter + borderWidth), (tileDiameter + borderWidth));
                    }
                }
                processor.process(tile);
                //I need to attach this (processed) tile to it's correct location in finalImg. How!??
            }
        }

请帮帮忙!

推荐答案

您可以使用 Canvas.drawBitmap 绘制处理瓦回
结果位图。使用功能是这样的:

You can use Canvas.drawBitmap to draw the processed tile back to the result bitmap. Use the function like this:

Canvas canvas = new Canvas(finalImg);
canvas.drawBitmap(tile,
                  null,
                  new Rect(x, y,
                           x + tileDiameter, y + tileDiameter),
                  null);

另请注意,你可能需要获得瓷砖的可变副本,因为
你得到一个 Bitmap.createBitmap 是不可改变的。

Also notice that you probably need to get a mutable copy of tile since the one you get from Bitmap.createBitmap is immutable.

这篇关于拆分和重新加入一个位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆