mirror of
https://github.com/zxing/zxing.git
synced 2025-02-02 05:41:08 -08:00
Issue 1175 fix toBitmap() to ignore padding bytes
git-svn-id: https://zxing.googlecode.com/svn/trunk@2217 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
b73a4af77b
commit
99d4b42b91
|
@ -131,44 +131,44 @@ namespace com.google.zxing.common
|
||||||
return result.ToString();
|
return result.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts this ByteMatrix to a black and white bitmap.
|
/// Converts this ByteMatrix to a black and white bitmap.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>A black and white bitmap converted from this ByteMatrix.</returns>
|
/// <returns>A black and white bitmap converted from this ByteMatrix.</returns>
|
||||||
public Bitmap ToBitmap()
|
public Bitmap ToBitmap()
|
||||||
{
|
{
|
||||||
const byte BLACK = 0;
|
const byte BLACK = 0;
|
||||||
const byte WHITE = 255;
|
const byte WHITE = 255;
|
||||||
sbyte[][] array = this.Array;
|
int width = Width;
|
||||||
int width = this.Width;
|
int height = Height;
|
||||||
int height = this.Height;
|
|
||||||
byte[] pixels = new byte[width * height];
|
|
||||||
|
|
||||||
for (int y = 0; y < height; y++)
|
// create the bitmap and lock the bits because we need the stride
|
||||||
{
|
// which is the width of the image and possible padding bytes
|
||||||
int offset = y * width;
|
var bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
|
||||||
for (int x = 0; x < width; x++)
|
var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
|
||||||
{
|
try
|
||||||
pixels[offset + x] = array[y][x] == 0 ? BLACK : WHITE;
|
{
|
||||||
}
|
var pixels = new byte[bmpData.Stride*height];
|
||||||
}
|
|
||||||
|
|
||||||
//Here create the Bitmap to the known height, width and format
|
for (int y = 0; y < height; y++)
|
||||||
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
|
{
|
||||||
|
var offset = y*bmpData.Stride;
|
||||||
|
for (var x = 0; x < width; x++)
|
||||||
|
{
|
||||||
|
pixels[offset + x] = this[x, y] ? BLACK : WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Create a BitmapData and Lock all pixels to be written
|
//Copy the data from the byte array into BitmapData.Scan0
|
||||||
BitmapData bmpData =
|
Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
|
||||||
bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
|
}
|
||||||
ImageLockMode.WriteOnly, bmp.PixelFormat);
|
finally
|
||||||
|
{
|
||||||
|
//Unlock the pixels
|
||||||
|
bmp.UnlockBits(bmpData);
|
||||||
|
}
|
||||||
|
|
||||||
//Copy the data from the byte array into BitmapData.Scan0
|
return bmp;
|
||||||
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