C\#QrCode.Net 을 이용 하여 QR 코드(Qr 코드)를 만 드 는 방법

4897 단어 qrcodeQR 코드
현재 인터넷 의 많은 응용 프로그램 은 QR 코드 로 인터넷 주소 나 다른 정 보 를 공유 하고 있다.특히 모 바 일 분야 에서 QR 코드 는 응용 장면 이 크다.프로젝트 의 수요 로 인해 사이트 에 QR 코드 를 생 성하 여 인터넷 주 소 를 분석 하 는 기능 을 추가 해 야 하기 때문에 구 글 이 큰 쥐 가 난 상황 에서 바 이 두 를 사용 할 수 밖 에 없다.바 이 두 N 이 많 습 니 다.일부 항목 을 찾 았 지만 가용성 이 강하 지 않 습 니 다.(VS 2005 로 개 발 된 프로젝트 가 있 는데 2010 년 에는 디 버 깅 이 안 됩 니 다.)마침내 codeplex 에서'신기'를 찾 았 습 니 다.이'신기'는 QR 코드 를 편리 하 게 생 성 할 수 있 습 니 다.속도 가 상당히 빠 르 고 중국 어 를 지원 하 며 MIT 협 의 를 따 를 수 있 습 니 다.
QrCode.Net 은 C\#를 사용 하여 QR 코드 그림 을 만 드 는 라 이브 러 리 입 니 다.WinForm,WebForm,WPF,Silverlight,Windows Phone 7 프로그램 에 QR 코드 인 코딩 출력 기능 을 편리 하 게 제공 할 수 있 습 니 다.QR 코드 파일 을 eps 형식 으로 내 보 낼 수 있 습 니 다.
프로젝트 주소:http://qrcodenet.codeplex.com
QrCode.Net 은 더 이상http://code.google.com/p/zxing/ ZXing포트 를 사용 하지 않 습 니 다.새로운 버 전 은 더 좋 은 성능 을 가 질 것 입 니 다.
테스트 결 과 는 다음 과 같 습 니 다(초 단위):
입력 문자열 길이:74 개
EC performance 1000 Tests~ QrCode.Net: 3929 ZXing: 5221
또한,QrCode.Net 은 문자열 을 분석 하여 UTF-8 인 코딩 을 사용 할 지 여 부 를 결정 할 수 있 습 니 다.(예 를 들 어 중국 어 를 사용 할 때.)
QrCode 사용 방법:
새 항목 은 라 이브 러 리 에 대한 인용 을 추가 한 다음 Gma.QrCodeNet.Encoding 네 임 스페이스 를 도입 합 니 다.
using Gma.QrCodeNet.Encoding;
콘 솔 에서 QR 코드 출력:

Console.Write(@"Type some text to QR code: ");
string sampleText = Console.ReadLine();
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode = qrEncoder.Encode(sampleText);
for (int j = 0; j < qrCode.Matrix.Width; j++)
{
for (int i = 0; i < qrCode.Matrix.Width; i++)
{
char charToPrint = qrCode.Matrix[i, j] ? '' : ' ';
Console.Write(charToPrint);
}
Console.WriteLine();
}
Console.WriteLine(@"Press any key to quit.");
Console.ReadKey();
이 코드 는 다음 과 같은 출력 을 생 성 합 니 다:

Graphics 에 QR 코드 그리 기:

const string helloWorld = "Hello World!";
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = qrEncoder.Encode(helloWorld);
const int moduleSizeInPixels = 5;
Renderer renderer = new Renderer(moduleSizeInPixels, Brushes.Black, Brushes.White);
Panel panel = new Panel();
Point padding = new Point(10,16);
Size qrCodeSize = renderer.Measure(qrCode.Matrix.Width);
panel.AutoSize = false;
panel.Size = qrCodeSize + new Size(2 * padding.X, 2 * padding.Y);
using (Graphics graphics = panel.CreateGraphics())
{
renderer.Draw(graphics, qrCode.Matrix, padding);
}
Writeable Bitmap 에 QR 코드 그리 기:

const string helloWorld = "Hello World!";
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(helloWorld, out qrCode);
const int moduleSizeInPixels = 5;
Renderer renderer = new Renderer(moduleSizeInPixels); //Black&White is default colour for drawing QrCode
//Matrix under qrCode might be null if input string is null or empty. 21 module wide is version 1 QrCode's width. 
int pixelSize = qrCode.Matrix == null ? renderer.Measure(21) : renderer.Measure(qrCode.Matrix.Width);
WriteableBitmap wBitmap = new WriteableBitmap(pixelSize, pixelSize, 96, 96, PixelFormats.Gray8, null);
//If wBitmap is null value. renderer will create Gray8 Bitmap as default.
renderer.Draw(wBitmap, qrCode.Matrix); //Default offset position is (0, 0);
//Now you can put wBitmap to Image control's Source or use it to create image file. 
WinForm 이나 WPF 프로그램 에 QR 코드 를 보 여 주 려 면 라 이브 러 리 를 도구 상자 에 끌 어 다 놓 은 다음 창 에서 컨트롤 을 끌 어 낼 수 있 습 니 다.

QR 코드 를 파일 에 직접 저장 하기:

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(helloWorld, out qrCode);
Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);
renderer.CreateImageFile(qrCode.Matrix, @"c:\temp\HelloWorld.png", ImageFormat.Png);
스 트림 에 QR 코드 쓰기:

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(helloWorld, out qrCode);
Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);
MemoryStream ms = new MemoryStream();
renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.png);
위 에서 말 한 것 은 편집장 님 께 서 소개 해 주신 C\#QrCode.Net 을 사용 하여 QR 코드(Qr 코드)를 생 성 하 는 것 입 니 다.여러분 께 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 님 께 서 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기