在字节数组中垂直翻转位图的算法 [英] Algorithm to vertically flip a bitmap in a byte array

查看:25
本文介绍了在字节数组中垂直翻转位图的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个类,用于通过 Mono For Android 将位图打印到 Android 中的便携式蓝牙打印机.我的类用于从流中获取像素数据,以便以正确的格式将其发送到打印机.现在这个类很简单,它只是读取高度、宽度和每像素位数.

I am writing a class for printing bitmaps to a portable bluetooth printer in Android via Mono For Android. My class is used to obtain the pixel data from the stream so that is can be sent to the printer in the correct format. Right now the class is simple, it just reads the height, width, and bits per pixel.

它使用偏移量读取像素数据并将其返回给打印机.现在我只是在处理每像素 1 位的黑白图像.我正在使用的位图采用 Windows 格式.

Using the offset it reads and returns the pixel data to the printer. Right now I am just working with 1 bit per pixel black and white images. The bitmaps I am working with are in Windows format.

这是原图:

这是打印的结果,第一张图没有任何变换.而第二个是用下面的代码修改BitArray的结果:

Here is the result of printing, the first image is without any transformation. And the second one is the result of modifying the BitArray with the following code:

        BitArray bits = new BitArray(returnBytes);
        BitArray flippedBits = new BitArray(bits);

        for (int i = 0, j = bits.Length - 1; i < bits.Length; i++, j--)
        {
            flippedBits[i] = bits[j];
        }

我的问题是:

在使用字节数组时如何垂直翻转图像.我无法找到执行此操作的算法,所有示例似乎都建议使用我无法使用的已建立的图形库.

How do I flip the image vertically when I am working with a byte array. I am having trouble finding the algorithm for doing this, all examples seem to suggest using established graphics libraries which I cannot use.

我的位图保存在一个一维数组中,第一行是字节,然后是第二行,第三行等等

My Bitmap is saved in a 1 dimensional array, with the first rows bytes, then the second, third, etc.

推荐答案

你需要做这样的事情:

BitArray bits = new BitArray(returnBytes);
BitArray flippedBits = new BitArray(bits);

for (int i = 0; i < bits.Length; i += width) {
    for (int j = 0, k = width - 1; j < width; ++j, --k) {
        flippedBits[i + j] = bits[i + k];
    }
}

如果您需要倒置镜像,请使用以下代码:

If you need to mirror picture upside-down, use this code:

BitArray bits = new BitArray(returnBytes);
BitArray flippedBits = new BitArray(bits);

for (int i = 0, j = bits.Length - width; i < bits.Length; i += width, j -= width) {
    for (int k = 0; k < width; ++k) {
        flippedBits[i + k] = bits[j + k];
    }
}

这篇关于在字节数组中垂直翻转位图的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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