ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。 其提供了多种平台下的客户端包括:J2ME、J2SE和Android。
首先,在其官网https://img.zhankr.net/bhujpv4mqqs41722.Png, sFD.FileName);
//SaveFileDialog sFD = new SaveFileDialog();
//sFD.DefaultExt = “*.png|*.png”;
//sFD.AddExtension = true;
//try
//{
// if (sFD.ShowDialog() == DialogResult.OK)
// {
// }
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}}
public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
{
Bitmap bmap = toBitmap(matrix);
bmap.Save(file, format);
}
public static Bitmap toBitmap(ByteMatrix matrix)
{
int width = matrix.Width;
int height = matrix.Height;
Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int x = ; x < width; x++)
{
for (int y = ; y < height; y++)
{
bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != – ? ColorTranslator.FromHtml(“0xFF000000”) : ColorTranslator.FromHtml(“0xFFFFFFFF”));
}
}
return bmap;
}
二维码的读取识别:
private void button1_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
Image img = Image.FromFile(this.openFileDialog1.FileName);
Bitmap bmap;
try
{
bmap = new Bitmap(img);
}
catch (System.IO.IOException ioe)
{
MessageBox.Show(ioe.ToString());
return;
}
if (bmap == null)
{
MessageBox.Show("Could not decode image");
return;
}
LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap);
}
catch(ReaderException re)
{
MessageBox.Show(re.ToString());
return;
} MessageBox.Show(result.Text);
}