ASP.NETがQRコードを生成する方法のまとめ

6072 ワード

この例ではASPについてまとめた.NETがQRコードを生成する方法.皆さんの参考にしてください.具体的には以下の通りです.
一例のc#がQRコードを生成するコードを共有し、ThoughtWorksを直接参照する.QRCode.dllクラスはQRコードを生成し、必要な友达が参考にしてください.
方法1.直接参照QRCode.dllクラス、QRコードを生成します.
コードの例:

ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new QRCodeEncoder();
encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//    (  :BYTE     ,ALPHA_NUMERIC         )
encoder.QRCodeScale = 4;//  
encoder.QRCodeVersion = 0;//  (  :   0                  )
encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
String qrdata = "     ";
System.Drawing.Bitmap bp = encoder.Encode(qrdata.ToString(), Encoding.GetEncoding("GB2312"));
Image image = bp;
Object oMissing = System.Reflection.Missing.Value;
pictureBox1.Image = bp;


QR画像を保存するには:
コードの例:

SaveFileDialog sf = new SaveFileDialog();
sf.Title = "        ";
sf.Filter = "    (*.jpg) |*.jpg|    (*.*) |*.*";
//            
sf.FilterIndex = 1;
//                
sf.RestoreDirectory = true;
if (sf.ShowDialog() == DialogResult.OK)
{
  Image im = this.pictureBox1.Image;
  //      
  string localFilePath = sf.FileName.ToString();
  if (sf.FileName != "")
  {
    string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//     ,    
    // newFileName = fileNameExt+DateTime.Now.ToString("yyyyMMdd") ;//         
    string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf(".")); //      ,    ,    
    string fn = sf.FileName;
    pictureBox1.Image.Save(FilePath +"-"+ DateTime.Now.ToString("yyyyMMdd") + ".jpg");
  }
}
//       
// QRCodeDecoder decoder = new QRCodeDecoder();
// String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image)));
//this.label3.Text = decodedString;


方法2.ZXingクラスライブラリを参照します.
ZXingは、複数のフォーマットの1 D/2 Dバーコードを解析するためのオープンソースJavaクラスライブラリです.QR符号化、Data Matrix、UPCの1 Dバーコードを復号できることが目標です.同時に、cpp、ActionScript、android、iPhone、rim、j 2 me、j 2 se、jruby、C#などのクラスライブラリも提供しています.zxingクラスライブラリの役割は主に復号であり、現在のオープンソースクラスライブラリの復号能力が比較的強い(商業的には別だが、ともすれば何千ものクラスライブラリの授権費用に対しては、確かに価値がある).
Google codeに対応するコードをダウンロード
1.zxingの最新パッケージをダウンロードする
zxingのホームページへ:http://code.google.com/p/zxing/
その中のCSharpフォルダを見つけて、vsの中で開いてコンパイルして、objの下でdebugの中のzxing.dllプロジェクトのbinファイルディレクトリにコピーして貼り付け、プロジェクトリファレンスの追加を右クリックします.を選択します.dllはプロジェクトに参照され、必要な場所で使用できます.
ソースコードにはUTF-8の問題が2箇所あり、中国語が文字化けしてしまう(コンパイル.dll前に修正)
その1:com.google.zxing.qrcode.encoder.Encoderクラスの

internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";

ここで、ISO-8859-1をUTF-8に変更
その2:com.google.zxing.qrcode.decoder.DecodedBitStreamParserクラスのメンバー
private const System.String UTF8 = "UTF8";
UTF 8をUTF-8に変更する必要があります
コードの例:

using com.google.zxing.qrcode;
using com.google.zxing;
using com.google.zxing.common;
using ByteMatrix = com.google.zxing.common.ByteMatrix;
using EAN13Writer = com.google.zxing.oned.EAN13Writer;
using EAN8Writer = com.google.zxing.oned.EAN8Writer;
using MultiFormatWriter = com.google.zxing.MultiFormatWriter;


方法:

string content = "     ";
ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
Bitmap bitmap = toBitmap(byteMatrix);
pictureBox1.Image = bitmap;
SaveFileDialog sFD = new SaveFileDialog();
sFD.Filter = "    (*.png) |*.png|    (*.*) |*.*";
sFD.DefaultExt = "*.png|*.png";
sFD.AddExtension = true;
if (sFD.ShowDialog() == DialogResult.OK)
{
if (sFD.FileName != "")
{
  writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
}
}


QRコードの解析:
コードの例:

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 bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap1);
}
catch (ReaderException re)
{
MessageBox.Show(re.ToString());
return;
}
MessageBox.Show(result.Text);
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 = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
  bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
}
}
return bmap;
}


PS:ここでは、非常に強力なQRコードのオンライン生成ツールをお勧めします.無料で使用できます.
オンラインでQRコードを生成するツール(強化版):http://tools.jb51.net/transcoding/jb51qrcode
もっとaspについてNet関連内容に興味のある読者は、「asp.net文字列操作テクニックまとめ」、「asp.net操作XMLテクニックまとめ」、「asp.netファイル操作テクニックまとめ」、「asp.net ajaxテクニックまとめ」、「asp.netキャッシュ操作テクニックまとめ」を参照してください.
本文で述べたように皆さんにasp.Netプログラミングが役立ちます.