BEGIN:VCARD VERSION:3.0 FN:..., . N:;;;.,... ORG: URL:http://ru.wikipedia.org/_ EMAIL;TYPE=INTERNET:vasya.kvakin@example.com END:VCARD
GEO:30.31616, 59.95015
PM> Install-Package MessagingToolkit.QRCode
using System.Text; using MessagingToolkit.QRCode.Codec; namespace EmbroideryFile.QRCode { internal class QRCodeCreator { public bool[][] GetQRCodeMatrix(string DataToEncode) { if (string.IsNullOrEmpty(DataToEncode)) return new bool[1][]; QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.CharacterSet = "UTF8"; qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeScale = 1; qrCodeEncoder.QRCodeVersion = -1; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; return qrCodeEncoder.CalQrcode(Encoding.UTF8.GetBytes(DataToEncode)); } } }
public struct Line { public Coords Dot1 { get; set; } public Coords Dot2 { get; set; } public int Length { get; set; } public bool Lowest { get; set; } }
private List<List<Coords>> GenerateQRCodeStitchesBoxed() { var blocks = new List<List<Coords>>(); int rectSize = GetRectSize(); // blocks.AddRange(GetRectangleSatin(0, 0, rectSize - 1, rectSize - 1)); // blocks.Add(GenerateBoxStitchBlock(2, 2, rectSize - 4)); // blocks.AddRange(GetSatinStitches(GetLaneList(0, rectSize + 1, rectSize, _dimension - rectSize - 1))); // blocks.AddRange(GetRectangleSatin(0, _dimension - rectSize, rectSize - 1, _dimension - 1)); // blocks.Add(GenerateBoxStitchBlock(2, _dimension - rectSize + 2, rectSize - 4)); // blocks.AddRange(GetSatinStitches(GetLaneList(rectSize + 1, 0, _dimension - rectSize - 1, _dimension - 1))); // blocks.AddRange(GetRectangleSatin(_dimension - rectSize, 0, _dimension - 1, rectSize - 1)); // blocks.Add(GenerateBoxStitchBlock(_dimension - rectSize + 2, 2, rectSize - 4)); // blocks.AddRange(GetSatinStitches(GetLaneList(_dimension - rectSize, rectSize + 1, _dimension - 1, _dimension - 1))); return blocks; }
IEnumerable<List<Coords>> GetRectangleSatin(int x1, int y1, int x2, int y2) { int LeftX = (x1 > x2) ? x2 : x1; int TopY = (y1 > y2) ? y2 : y1; int RightX = (x1 < x2) ? x2 : x1; var BottomY = (y1 < y2) ? y2 : y1; int length = RightX - LeftX; var rect = new List<List<Coords>>(); rect.Add(GenerateVerticalColumnStitchBlock(LeftX, TopY, length)); rect.Add(GenerateHorizonColumnStitchBlock(LeftX, BottomY, length)); rect.Add(ReverseCoords(GenerateVerticalColumnStitchBlock(RightX, TopY + 1, length))); rect.Add(ReverseCoords(GenerateHorizonColumnStitchBlock(LeftX + 1, TopY, length))); return rect; }
/// <summary> /// /// </summary> /// <param name="cellHorizonPos"> </param> /// <param name="cellVerticalPos"> </param> /// <param name="boxSize"> </param> /// <returns> </returns> private List<Coords> GenerateBoxStitchBlock(int cellHorizonPos, int cellVerticalPos, int boxSize) { var block = new List<Coords>(); int y = 0; int x = 0; int startX = cellHorizonPos * _cellSize; int startY = cellVerticalPos * _cellSize; block.Add(new Coords { X = startX, Y = startY }); while (y < _cellSize * boxSize) { while (x < _cellSize * boxSize - _dX) { x = x + _dX; block.Add(new Coords{ X = startX + x, Y = startY + y }); } x = boxSize * _cellSize; block.Add(new Coords { X = startX + x, Y = startY + y }); y = y + _dY; while (x > _dX) { x = x - _dX; block.Add(new Coords { X = startX + x, Y = startY + y }); } x = 0; block.Add(new Coords { X = startX + x, Y = startY + y }); y = y + _dY; } return block; }
/// <summary> /// /// </summary> private List<List<Coords>> GetSatinStitches(List<Line> lanes) { List<List<Coords>> blockList = new List<List<Coords>>(); foreach (var lane in lanes) { List<Coords> satin = null; if (((lane.Length == 1) && ((lane.Dot1.X % 2) == 0)) || ((lane.Length > 1) && (lane.Dot2.Y > lane.Dot1.Y))) satin = GenerateVerticalColumnStitchBlock(lane.Dot1.X, lane.Dot1.Y, lane.Length); else satin = ReverseCoords(GenerateVerticalColumnStitchBlock(lane.Dot2.X, lane.Dot2.Y, lane.Length)); blockList.Add(satin); } return blockList; }
/// <summary> /// /// </summary> /// <param name="x1">X </param> /// <param name="y1">Y </param> /// <param name="x2">X </param> /// <param name="y2">Y </param> /// <returns></returns> private List<Line> GetLaneList(int x1, int y1, int x2, int y2) { try { if (_lines != null) _lines.Clear(); if (y1 > y2) { _topY = y2; _bottomY = y1; } else { _topY = y1; _bottomY = y2; } if (x1 > x2) { _leftX = x2; _rightX = x1; } else { _leftX = x1; _rightX = x2; } for (int j = _leftX; j <= _rightX; j = j + 2) //X { _state = false; for (int i = _topY; i <= _bottomY; i++) // Y { ConsumeRelativeCellDown(j, i); } if (j >= _rightX) break; _state = false; for (int i = _bottomY; i >= _topY; i--) // Y { ConsumeRelativeCellUp(j + 1, i); } } return _lines; } catch (Exception ex) { Trace.WriteLine(string.Format("GetLineList(): {0}",ex)); throw; } }
/// <summary> /// /// </summary> /// <param name="j"></param> /// <param name="i"></param> void ConsumeRelativeCellDown(int j, int i) { if (_cells[j][i] == true) { // if ((i == _topY)) { _dot1 = new Coords() { X = j, Y = i }; _curLane.Dot1 = _dot1; _laneLen = 1; _state = true; } else if ((_state == false)) { // if (i == _bottomY) { _dot1 = new Coords() { X = j, Y = i }; _curLane.Dot1 = _dot1; _dot2 = new Coords() { X = j, Y = i }; _curLane.Dot2 = _dot2; _curLane.Length = 1; _curLane.Lowest = true; _endLaneFlag = true; } // else { _dot1 = new Coords() { X = j, Y = i }; _curLane.Dot1 = _dot1; _laneLen = 1; _state = true; } } else if ((i == _bottomY)) { // _dot2 = new Coords() { X = j, Y = i }; _curLane.Dot2 = _dot2; _curLane.Length = ++_laneLen; _curLane.Lowest = true; _endLaneFlag = true; } // else { _laneLen++; } } // , else if (_state == true) { _dot2 = new Coords() { X = j, Y = i - 1 }; _curLane.Dot2 = _dot2; _curLane.Length = _laneLen; _state = false; _endLaneFlag = true; } if (_endLaneFlag == true) { _lines.Add(_curLane); _endLaneFlag = false; } }
void ConsumeRelativeCellUp(int j, int i) { if (_cells[j][i] == true) { // if ((i == _bottomY)) { _dot1 = new Coords { X = j, Y = i }; _curLane.Dot1 = _dot1; _laneLen = 1; _state = true; } else if ((_state == false)) { // if (i == _topY) { _dot1 = new Coords { X = j, Y = i }; _curLane.Dot1 = _dot1; _dot2 = new Coords { X = j, Y = i }; _curLane.Dot2 = _dot2; _curLane.Length = 1; _curLane.Lowest = true; _endLaneFlag = true; } // else { _dot1 = new Coords { X = j, Y = i }; _curLane.Dot1 = _dot1; _laneLen = 1; _state = true; } } else if ((i == _topY)) { // end of lane at the top _dot2 = new Coords { X = j, Y = i }; _curLane.Dot2 = _dot2; _curLane.Length = ++_laneLen; _curLane.Lowest = true; _endLaneFlag = true; } // else { _laneLen++; } } // , else if (_state) { _dot2 = new Coords { X = j, Y = i + 1 }; _curLane.Dot2 = _dot2; _curLane.Length = _laneLen; _state = false; _endLaneFlag = true; } if (_endLaneFlag) { _lines.Add(_curLane); _endLaneFlag = false; } }
/// <summary> /// /// </summary> /// <param name="cellHorizonPos"> </param> /// <param name="cellVerticalPos"> </param> /// <param name="length"></param> private List<Coords> GenerateVerticalColumnStitchBlock(int cellHorizonPos, int cellVerticalPos, int length) { var block = new List<Coords>(); int curX, curY; int columnLength = _cellSize * length; int startX = cellHorizonPos * _cellSize; int startY = cellVerticalPos * _cellSize; block.Add(new Coords { X = startX + _cellSize, Y = startY }); for (curY = 0; curY < columnLength; curY = curY + _dY) { for (curX = (curY == 0) ? 0 : _dX; (curX < _cellSize) && (curY < columnLength); curX = curX + _dX) { block.Add(new Coords { X = startX + curX, Y = startY + curY }); curY = curY + _dY; } int edgedX = _cellSize - (curX - _dX); int edgedY = edgedX * _dY / _dX; curX = _cellSize; curY = curY + edgedY - _dY; block.Add(new Coords { X = startX + curX, Y = startY + curY }); curY = curY + _dY; for (curX = _cellSize - _dX; (curX > 0) && (curY < columnLength); curX = curX - _dX) { block.Add(new Coords { X = startX + curX, Y = startY + curY }); curY = curY + _dY; } edgedX = curX + _dX; edgedY = edgedX * _dY / _dX; curY = curY + edgedY - _dY; block.Add(new Coords { X = startX, Y = startY + curY }); } curX = _cellSize; curY = columnLength; block.Add(new Coords { X = startX + curX, Y = startY + curY }); return block; }
public QrcodeDst() { _dst = new DstFile(); _stitchGen = new QrCodeStitcher(); }
public QRCodeStitchInfo QrStitchInfo { set { _stitchGen.Info = value; } }
var qrcodeGen = new QrcodeDst(); using (var inputStreamReader = new StreamReader(fileName)) { var text = inputStreamReader.ReadToEnd(); using (Stream outStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) { if (qrcodeGen != null) { qrcodeGen.QrStitchInfo = new QRCodeStitchInfo {QrCodeText = text}; qrcodeGen.FillStream(outStream); } } }
Bit number | 7 | 6 | five | four | 3 | 2 | one | 0 |
Byte 1 | y + 1 | y-1 | y + 9 | y-9 | x-9 | x + 9 | x-1 | x + 1 |
Byte 2 | y + 3 | y-3 | y + 27 | y-27 | x-27 | x + 27 | x-3 | x + 3 |
Byte 3 | transition | stop | y + 81 | y-81 | x-81 | x + 81 | Always installed | Always installed |
byte[] encode_record(int x, int y, DstStitchType stitchType) { byte b0, b1, b2; b0 = b1 = b2 = 0; byte[] b = new byte[3]; // >+121 or < -121. if (x >= +41) { b2 += setbit(2); x -= 81; }; if (x <= -41) { b2 += setbit(3); x += 81; }; if (x >= +14) { b1 += setbit(2); x -= 27; }; if (x <= -14) { b1 += setbit(3); x += 27; }; if (x >= +5) { b0 += setbit(2); x -= 9; }; if (x <= -5) { b0 += setbit(3); x += 9; }; if (x >= +2) { b1 += setbit(0); x -= 3; }; if (x <= -2) { b1 += setbit(1); x += 3; }; if (x >= +1) { b0 += setbit(0); x -= 1; }; if (x <= -1) { b0 += setbit(1); x += 1; }; if (x != 0) { throw; }; if (y >= +41) { b2 += setbit(5); y -= 81; }; if (y <= -41) { b2 += setbit(4); y += 81; }; if (y >= +14) { b1 += setbit(5); y -= 27; }; if (y <= -14) { b1 += setbit(4); y += 27; }; if (y >= +5) { b0 += setbit(5); y -= 9; }; if (y <= -5) { b0 += setbit(4); y += 9; }; if (y >= +2) { b1 += setbit(7); y -= 3; }; if (y <= -2) { b1 += setbit(6); y += 3; }; if (y >= +1) { b0 += setbit(7); y -= 1; }; if (y <= -1) { b0 += setbit(6); y += 1; }; if (y != 0) { throw; }; switch (stitchType) { case DstStitchType.NORMAL: b2 += (byte)3; break; case DstStitchType.END: b2 = (byte)243; b0 = b1 = (byte)0; break; case DstStitchType.JUMP: b2 += (byte)131; break; case DstStitchType.STOP: b2 += (byte)195; break; default: b2 += 3; break; }; b[0] = b0; b[1] = b1; b[2] = b2; return b; }
qrcodegen.exe test.asc
Source: https://habr.com/ru/post/257763/
All Articles