IronQR에서 QR 코드를 SVG로 저장하기
IronQR는 QR 코드를 SVG로 직접 내보내지 않습니다. 생성된 비트맵을 타사 라이브러리로 SVG 사각형으로 다시 그려 벡터 파일을 생성할 수 있습니다.
그 이유는 기본 이미지 스택 때문입니다. IronQR은 이미지 처리를 위해 SixLabors.ImageSharp을 사용하며, PNG, JPEG, BMP와 같은 래스터 형식을 처리하지만 SVG 출력을 제공하지 않습니다. SVG는 의존성이 지원하는 대상 형식이 아닙니다.
해결책
IronQR을 사용하여 QR 코드를 정상적으로 생성한 후, 픽셀을 검사하고 매트릭스를 벡터 형태로 재구성해주세요 Svg.NET을 사용하여. IronQR의 QR 생성 기능을 유지하면서 SVG의 스타일링과 구조에 대한 전체 제어를 얻을 수 있습니다.
1. QR 코드 생성 및 비트맵으로 변환
QR 코드를 작성한 후 그것을 AnyBitmap에 저장하고 이를 System.Drawing.Bitmap로 캐스트하여 각 픽셀을 개별적으로 읽을 수 있게 합니다.
var qr = QrWriter.Write("Hello SVG");
var anyBitmap = qr.Save();
// Convert AnyBitmap to System.Drawing.Bitmap
System.Drawing.Bitmap bit = anyBitmap;
var qr = QrWriter.Write("Hello SVG");
var anyBitmap = qr.Save();
// Convert AnyBitmap to System.Drawing.Bitmap
System.Drawing.Bitmap bit = anyBitmap;
Dim qr = QrWriter.Write("Hello SVG")
Dim anyBitmap = qr.Save()
' Convert AnyBitmap to System.Drawing.Bitmap
Dim bit As System.Drawing.Bitmap = anyBitmap
2. SVG 사각형으로 픽셀 다시 그리기
비트맵 크기에 맞게 설정된 SvgDocument을 생성한 후 픽셀을 순회하며 픽셀이 검은색으로 읽히는 곳에 1x1 SvgRectangle를 추가합니다. 임계값 검사(R, G, 및 B 모두 128 이하)로 인해 거의 검은 픽셀을 채워진 셀로 간주합니다.
// Create new SVG document
var svgDoc = new SvgDocument
{
Width = bit.Width,
Height = bit.Height
};
// Loop through bitmap pixels and add black pixels as SVG rectangles
for (int y = 0; y < bit.Height; y++)
{
for (int x = 0; x < bit.Width; x++)
{
System.Drawing.Color pixel = bit.GetPixel(x, y);
// Check if pixel is black (or close enough)
if (pixel.R < 128 && pixel.G < 128 && pixel.B < 128)
{
var rect = new SvgRectangle
{
X = x,
Y = y,
Width = 1,
Height = 1,
Fill = new SvgColourServer(System.Drawing.Color.Black)
};
svgDoc.Children.Add(rect);
}
}
}
// Create new SVG document
var svgDoc = new SvgDocument
{
Width = bit.Width,
Height = bit.Height
};
// Loop through bitmap pixels and add black pixels as SVG rectangles
for (int y = 0; y < bit.Height; y++)
{
for (int x = 0; x < bit.Width; x++)
{
System.Drawing.Color pixel = bit.GetPixel(x, y);
// Check if pixel is black (or close enough)
if (pixel.R < 128 && pixel.G < 128 && pixel.B < 128)
{
var rect = new SvgRectangle
{
X = x,
Y = y,
Width = 1,
Height = 1,
Fill = new SvgColourServer(System.Drawing.Color.Black)
};
svgDoc.Children.Add(rect);
}
}
}
Imports System.Drawing
' Create new SVG document
Dim svgDoc As New SvgDocument With {
.Width = bit.Width,
.Height = bit.Height
}
' Loop through bitmap pixels and add black pixels as SVG rectangles
For y As Integer = 0 To bit.Height - 1
For x As Integer = 0 To bit.Width - 1
Dim pixel As Color = bit.GetPixel(x, y)
' Check if pixel is black (or close enough)
If pixel.R < 128 AndAlso pixel.G < 128 AndAlso pixel.B < 128 Then
Dim rect As New SvgRectangle With {
.X = x,
.Y = y,
.Width = 1,
.Height = 1,
.Fill = New SvgColourServer(Color.Black)
}
svgDoc.Children.Add(rect)
End If
Next
Next
3. SVG 파일 작성
모든 검은색 셀이 추가되면 문서를 디스크에 저장합니다.
// Save the result as SVG
svgDoc.Write("qr-from-ironqr.svg");
// Save the result as SVG
svgDoc.Write("qr-from-ironqr.svg");
' Save the result as SVG
svgDoc.Write("qr-from-ironqr.svg")
이렇게 하면 인쇄나 고해상도 사용에 적합한 확장 가능한 벡터 기반 QR 코드를 얻을 수 있습니다.

