Skip to content

Instantly share code, notes, and snippets.

@mdeterman
Last active December 7, 2015 18:45
Show Gist options
  • Save mdeterman/da3882c386128e466f5a to your computer and use it in GitHub Desktop.
Save mdeterman/da3882c386128e466f5a to your computer and use it in GitHub Desktop.
Mirlin ConvertBitmapToByteArray
private byte[] ConvertBitmapToByteArray(Bitmap bitmap)
{
byte[] byteArray = new byte[bitmap.Width * bitmap.Height];
for (int i = 0; i < bitmap.Width; i++)
{
for (int x = 0; x < bitmap.Height; x++)
{
//Get the Pixel
Color pixel = bitmap.GetPixel(i, x);
if (pixel.R == pixel.G && pixel.G == pixel.B)
{
byteArray[(x * bitmap.Width) + i] = pixel.R;
}
else
{
// Only monochrome images are supported by MIRLIN. However, it's possible to convert a colour
// image to monochrome using a formula such as x=0.3R + 0.59G + 0.11B where R=Red, G=Green, B=Blue.
// This approach is used with risk and should be thoroughly tested. Please contact FotoNation UK
// for further information.
throw new NotSupportedException("Only monochrome images are supported by MIRLIN.");
}
}
}
return byteArray;
}
public static byte[] convertBitmapToByteArray(BufferedImage image) {
byte[] byteArray = new byte[image.getWidth() * image.getHeight()];
for(int i=0; i<image.getWidth(); i++) {
for(int x=0; x<image.getHeight(); x++) {
Color color = new Color(image.getRGB(i, x));
if (color.getRed() == color.getGreen() && color.getGreen() == color.getBlue())
{
byteArray[(x * image.getWidth()) + i] = (byte) color.getRed();
} else {
// Only monochrome images are supported by MIRLIN. However, it's possible to convert a colour
// image to monochrome using a formula such as x=0.3R + 0.59G + 0.11B where R=Red, G=Green, B=Blue.
// This approach is used with risk and should be thoroughly tested. Please contact FotoNation UK
// for further information.
throw new RuntimeException("Only monochrome images are supported by MIRLIN.");
}
}
}
return byteArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment