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

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

问题描述

我写一个类的位图打印通过单声道中的Andr​​oid便携式蓝牙打印机为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具有以下code的结果是:

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

我的问题是:

My Question is:

我如何翻转图像的垂直,当我用一个字节数组工作。我无法找到的算法,这样一来,所有的例子似乎可以用建立图形库,我不能用建议。

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.

编辑:

我的位图被保存在一个1维阵列,与第一行字节,然后是第二,第三,等。

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

如果您需要镜像画面上下颠倒,使用code:

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天全站免登陆