【问题标题】:How to divide a bitmap into parts that are bitmaps too如何将位图划分为位图的部分
【发布时间】:2013-11-01 13:56:01
【问题描述】:

我需要一些关于将位图分成小块的可能方法的信息。

更重要的是,我需要一些选项来判断。我检查了很多帖子,但我仍然不完全相信该怎么做:

  1. cut the portion of bitmap
  2. How do I cut out the middle area of the bitmap?

这两个帖子是我找到的一些不错的选择,但是我无法计算每种方法的 CPU 和 RAM 成本,或者我根本不应该为这个计算而烦恼。尽管如此,如果我要去做某事,为什么不从一开始就以最好的方式去做呢。

如果能获得一些关于位图压缩的提示和链接,我将不胜感激,这样结合这两种方法可能会获得更好的性能。

【问题讨论】:

    标签: android android-bitmap


    【解决方案1】:

    此功能允许您将位图拆分为行数和列数。

    示例位图[][] 位图 = splitBitmap(bmp, 2, 1); 将创建存储在二维数组中的垂直分割位图。 2列1行

    示例位图[][] 位图 = splitBitmap(bmp, 2, 2); 将一个位图拆分为四个位图,存储在一个二维数组中。 2列2行

    public Bitmap[][] splitBitmap(Bitmap bitmap, int xCount, int yCount) {
        // Allocate a two dimensional array to hold the individual images.
        Bitmap[][] bitmaps = new Bitmap[xCount][yCount];
        int width, height;
        // Divide the original bitmap width by the desired vertical column count
        width = bitmap.getWidth() / xCount;
        // Divide the original bitmap height by the desired horizontal row count
        height = bitmap.getHeight() / yCount;
        // Loop the array and create bitmaps for each coordinate
        for(int x = 0; x < xCount; ++x) {
            for(int y = 0; y < yCount; ++y) {
                // Create the sliced bitmap
                bitmaps[x][y] = Bitmap.createBitmap(bitmap, x * width, y * height, width, height);
            }
        }
        // Return the array
        return bitmaps;     
    }
    

    【讨论】:

    • 回答的时候尽量解释一下,而不是给出一段代码。
    • 请用一些文字来回答您的答案,说明您将来会做什么。
    • 工作,但实际上,结果是图像的转置矩阵。
    【解决方案2】:

    您想将位图分成几部分。我假设您想从位图中剪切相等的部分。比如说你需要一个位图的四个相等的部分。

    这是一种将位图分成四等份并将其放在位图数组中的方法。

    public Bitmap[] splitBitmap(Bitmap src) {
        Bitmap[] divided = new Bitmap[4];
        imgs[0] = Bitmap.createBitmap(
            src,
            0, 0,
            src.getWidth() / 2, src.getHeight() / 2
        );
        imgs[1] = Bitmap.createBitmap(
            src,
            src.getWidth() / 2, 0,
            src.getWidth() / 2, src.getHeight() / 2
        );
        imgs[2] = Bitmap.createBitmap(
            src,
            0, src.getHeight() / 2,
            src.getWidth() / 2, src.getHeight() / 2
        );
        imgs[3] = Bitmap.createBitmap(
            src,
            src.getWidth() / 2, src.getHeight() / 2,
            src.getWidth() / 2, src.getHeight() / 2
        );
        return divided;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2022-10-16
      相关资源
      最近更新 更多