mirror of
https://github.com/zxing/zxing.git
synced 2025-01-27 11:01:00 -08:00
Issue 921 add ToBitmap
git-svn-id: https://zxing.googlecode.com/svn/trunk@1864 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
f8e3cef85d
commit
13f529aa80
|
@ -14,6 +14,9 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
using System;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
namespace com.google.zxing.common
|
||||
{
|
||||
|
||||
|
@ -127,5 +130,45 @@ namespace com.google.zxing.common
|
|||
}
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts this ByteMatrix to a black and white bitmap.
|
||||
/// </summary>
|
||||
/// <returns>A black and white bitmap converted from this ByteMatrix.</returns>
|
||||
public Bitmap ToBitmap()
|
||||
{
|
||||
const byte BLACK = 0;
|
||||
const byte WHITE = 255;
|
||||
sbyte[][] array = this.Array;
|
||||
int width = this.Width;
|
||||
int height = this.Height;
|
||||
byte[] pixels = new byte[width * height];
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
int offset = y * width;
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
ixels[offset + x] = array[y][x] == 0 ? BLACK : WHITE;
|
||||
}
|
||||
}
|
||||
|
||||
//Here create the Bitmap to the known height, width and format
|
||||
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
|
||||
|
||||
//Create a BitmapData and Lock all pixels to be written
|
||||
BitmapData bmpData =
|
||||
bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
|
||||
ImageLockMode.WriteOnly, bmp.PixelFormat);
|
||||
|
||||
//Copy the data from the byte array into BitmapData.Scan0
|
||||
Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
|
||||
|
||||
//Unlock the pixels
|
||||
bmp.UnlockBits(bmpData);
|
||||
|
||||
//Return the bitmap
|
||||
return bmp;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue