/*
* Copyright 2007 ZXing authors
* Copyright 2012 Rodrigo Reis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using com.google.zxing.common;
using System.Drawing.Imaging;
namespace com.google.zxing.qrcode.client
{
/// <summary> This implementation can detect and decode QR Codes
in an image.
///
/// </summary>
/// <author> Sean Owen
/// </author>
/// <author>
www.Redivivus.in (
suraj....@redivivus.in) - Ported
from ZXING Java Source
/// </author>
/// <author> Rodrigo Reis
/// </author>
/// <author>
www.thinkr.com.br (
rodri...@gmail.com) - Ported from
ZXING Java Source
/// </author>
public sealed class MatrixToImageWriter
{
/// <summary>
///
/// </summary>
private static Color BLACK = Color.FromArgb(0, 0, 0);
/// <summary>
///
/// </summary>
private static Color WHITE = Color.FromArgb(255, 255, 255);
/// <summary>
///
/// </summary>
private MatrixToImageWriter() { }
/// <summary>
/// Renders a BitMatrix as an image, where "false" bits are
rendered
/// as white, and "true" bits are rendered as black.
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
public static Bitmap toBufferedImage(BitMatrix matrix)
{
int width = matrix.Width;
int height = matrix.Height;
Bitmap image = new Bitmap(width, height);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
image.SetPixel(x, y, (matrix.get_Renamed(x, y) ?
BLACK : WHITE));
}
}
return image;
}
/// <summary>
/// Renders a ByteMatrix as an image, where "false" bits are
rendered
/// as white, and "true" bits are rendered as black.
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
public static Bitmap toBufferedImage(ByteMatrix matrix)
{
int width = matrix.Width;
int height = matrix.Height;
Bitmap image = new Bitmap(width, height);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
image.SetPixel(x, y, (matrix.get_Renamed(x,
y).Equals(0) ? BLACK : WHITE));
}
}
return image;
}
/// <summary>
///
/// </summary>
/// <param name="matrix"></param>
/// <param name="filepath"></param>
public static void writeToFile(BitMatrix matrix, ImageFormat
format, String filepath)
{
try
{
Bitmap image = toBufferedImage(matrix);
image.Save(filepath, format);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
///
/// </summary>
/// <param name="matrix"></param>
/// <param name="filepath"></param>
public static void writeToFile(ByteMatrix matrix, ImageFormat
format, String filepath)
{
try
{
Bitmap image = toBufferedImage(matrix);
image.Save(filepath, format);
}
catch (Exception ex)
{
throw ex;
}
}
}
}