Генерация QR кода ZXing
Необходимо сгенерировать QR-код из текста. Использую библиотеку ZXing.unity.dll
using ZXing;
using ZXing.QrCode;
Генерирую QR-код
private static Color32[] Encode(string textForEncoding, int width, int height) {
var writer = new BarcodeWriter {
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions{
Height = height,
Width = width
}
};
return writer.Write(textForEncoding);
}
public Texture2D generateQR(string text) {
var encoded = new Texture2D (256, 256);
var color32 = Encode(text, encoded.width, encoded.height);
encoded.SetPixels32(color32);
encoded.Apply();
return encoded;
}
Вешаю картинку на RawImagе, забитую в поле
public RawImage RI;
RI.texture = generateQR("https://test.link/123");
На выходе имею картинку с здоровенными белыми бордерами.
- Q1 - Как их убрать белые бордеры?
- Q2 - Есть ли возможность сделать прозрачный бекграунд?
- Q3 - Как заменить черный цвет кода, на другой?
Ответы (1 шт):
Автор решения: Michael
→ Ссылка
Q3: Пример изменения цвета фона и тона:
private static Color32[] Encode(string textForEncoding, int width, int height)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width
},
Renderer = new Color32Renderer
{
Background = new Color32(255, 0, 0, 0),
Foreground = new Color32(0, 255, 0, 0)
}
};
return writer.Write(textForEncoding);
}
Q2: Я не полностью уверен, но следующий пример должен создать прозрачный фон:
private static Color32[] Encode(string textForEncoding, int width, int height)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width
},
Renderer = new Color32Renderer
{
Background = new Color32(0, 0, 0, 255)
}
};
return writer.Write(textForEncoding);
}
Q1: Создайте изображение как можно меньше размером, затем измените размер вручную
private static Color32[] Encode(string textForEncoding, int width, int height)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = width,
Width = height,
Margin = 0
}
};
return writer.Write(textForEncoding);
}
public Texture2D generateQR(string text)
{
// create the image as small as possible without a white border by setting width an height to 1
var color32 = Encode(text, 1, 1);
var widthAndHeight = color32.Length / 2;
var encoded = new Texture2D(widthAndHeight, widthAndHeight);
encoded.SetPixels32(color32);
encoded.Apply();
//
// Attention: insert code for resizing to the desired size here
// I didn't try it myself. Not sure if it works.
//
encoded.Resize(256, 256);
return encoded;
}
