/// <summary> /// <p>Attempts to decode a one-dimensional barcode format given a single row of /// an image.</p> /// </summary> /// <param name="rowNumber">row number from top of the row</param> /// <param name="row">the black/white pixel data of the row</param> /// <param name="hints">decode hints</param> /// <returns> /// <see cref="Result"/>containing encoded string and start/end of barcode or null, if an error occurs or barcode cannot be found /// </returns> override public Result decodeRow(int rowNumber, BitArray row, IDictionary<DecodeHintType, object> hints) { Pair leftPair = decodePair(row, false, rowNumber, hints); addOrTally(possibleLeftPairs, leftPair); row.reverse(); Pair rightPair = decodePair(row, true, rowNumber, hints); addOrTally(possibleRightPairs, rightPair); row.reverse(); foreach (Pair left in possibleLeftPairs) { if (left.Count > 1) { foreach (Pair right in possibleRightPairs) { if (right.Count > 1) { if (checkChecksum(left, right)) { return constructResult(left, right); } } } } } return null; }
/// <summary> /// <p>Attempts to decode a one-dimensional barcode format given a single row of /// an image.</p> /// </summary> /// <param name="rowNumber">row number from top of the row</param> /// <param name="row">the black/white pixel data of the row</param> /// <param name="hints">decode hints</param> /// <returns> /// <see cref="Result"/>containing encoded string and start/end of barcode or null, if an error occurs or barcode cannot be found /// </returns> override public Result decodeRow(int rowNumber, BitArray row, IDictionary<DecodeHintType, object> hints) { Pair leftPair = decodePair(row, false, rowNumber, hints); addOrTally(possibleLeftPairs, leftPair); row.reverse(); Pair rightPair = decodePair(row, true, rowNumber, hints); addOrTally(possibleRightPairs, rightPair); row.reverse(); int lefSize = possibleLeftPairs.Count; for (int i = 0; i < lefSize; i++) { Pair left = possibleLeftPairs[i]; if (left.Count > 1) { int rightSize = possibleRightPairs.Count; for (int j = 0; j < rightSize; j++) { Pair right = possibleRightPairs[j]; if (right.Count > 1) { if (checkChecksum(left, right)) { return constructResult(left, right); } } } } } return null; }
/// <summary> /// Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees /// </summary> public void rotate180() { var topRow = new BitArray(width); var bottomRow = new BitArray(width); int maxHeight = (height + 1) / 2; for (int i = 0; i < maxHeight; i++) { topRow = getRow(i, topRow); int bottomRowIndex = height - 1 - i; bottomRow = getRow(bottomRowIndex, bottomRow); topRow.reverse(); bottomRow.reverse(); setRow(i, bottomRow); setRow(bottomRowIndex, topRow); } }
/// <summary> /// Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees /// </summary> public void rotate180() { var width = Width; var height = Height; var topRow = new BitArray(width); var bottomRow = new BitArray(width); for (int i = 0; i < (height + 1) / 2; i++) { topRow = getRow(i, topRow); bottomRow = getRow(height - 1 - i, bottomRow); topRow.reverse(); bottomRow.reverse(); setRow(i, bottomRow); setRow(height - 1 - i, topRow); } }
/// <summary> /// Identify where the end of the middle / payload section ends. /// </summary> /// <param name="row">row of black/white values to search</param> /// <returns>Array, containing index of start of 'end block' and end of 'end /// block' or null, if nothing found</returns> private int[] decodeEnd(BitArray row) { // For convenience, reverse the row and then // search from 'the start' for the end block row.reverse(); int endStart = skipWhiteSpace(row); if (endStart < 0) return null; int[] endPattern = findGuardPattern(row, endStart, END_PATTERN_REVERSED); if (endPattern == null) { row.reverse(); return null; } // The start & end patterns must be pre/post fixed by a quiet zone. This // zone must be at least 10 times the width of a narrow line. // ref: http://www.barcode-1.net/i25code.html if (!validateQuietZone(row, endPattern[0])) { row.reverse(); return null; } // Now recalculate the indices of where the 'endblock' starts & stops to // accommodate // the reversed nature of the search int temp = endPattern[0]; endPattern[0] = row.Size - endPattern[1]; endPattern[1] = row.Size - temp; row.reverse(); return endPattern; }
/// <summary> /// We're going to examine rows from the middle outward, searching alternately above and below the /// middle, and farther out each time. rowStep is the number of rows between each successive /// attempt above and below the middle. So we'd scan row middle, then middle - rowStep, then /// middle + rowStep, then middle - (2 * rowStep), etc. /// rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily /// decided that moving up and down by about 1/16 of the image is pretty good; we try more of the /// image if "trying harder". /// </summary> /// <param name="image">The image to decode</param> /// <param name="hints">Any hints that were requested</param> /// <returns>The contents of the decoded barcode</returns> virtual protected Result doDecode(BinaryBitmap image, IDictionary<DecodeHintType, object> hints) { int width = image.Width; int height = image.Height; BitArray row = new BitArray(width); int middle = height >> 1; bool tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER); int rowStep = Math.Max(1, height >> (tryHarder ? 8 : 5)); int maxLines; if (tryHarder) { maxLines = height; // Look at the whole image, not just the center } else { maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image } for (int x = 0; x < maxLines; x++) { // Scanning from the middle out. Determine which row we're looking at next: int rowStepsAboveOrBelow = (x + 1) >> 1; bool isAbove = (x & 0x01) == 0; // i.e. is x even? int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow); if (rowNumber < 0 || rowNumber >= height) { // Oops, if we run off the top or bottom, stop break; } // Estimate black point for this row and load it: row = image.getBlackRow(rowNumber, row); if (row == null) continue; // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to // handle decoding upside down barcodes. for (int attempt = 0; attempt < 2; attempt++) { if (attempt == 1) { // trying again? row.reverse(); // reverse the row and continue // This means we will only ever draw result points *once* in the life of this method // since we want to avoid drawing the wrong points after flipping the row, and, // don't want to clutter with noise from every single row scan -- just the scans // that start on the center line. if (hints != null && hints.ContainsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK)) { IDictionary<DecodeHintType, Object> newHints = new Dictionary<DecodeHintType, Object>(); foreach (var hint in hints) { if (hint.Key != DecodeHintType.NEED_RESULT_POINT_CALLBACK) newHints.Add(hint.Key, hint.Value); } hints = newHints; } } // Look for a barcode Result result = decodeRow(rowNumber, row, hints); if (result == null) continue; // We found our barcode if (attempt == 1) { // But it was upside down, so note that result.putMetadata(ResultMetadataType.ORIENTATION, 180); // And remember to flip the result points horizontally. ResultPoint[] points = result.ResultPoints; if (points != null) { points[0] = new ResultPoint(width - points[0].X - 1, points[0].Y); points[1] = new ResultPoint(width - points[1].X - 1, points[1].Y); } } return result; } } return null; }
// Start is called before the first frame update void Start() { Texture2D texture = new Texture2D(500, 500); texture.filterMode = FilterMode.Bilinear; BarcodeWriterGeneric writer = new BarcodeWriterGeneric(); writer.Format = BarcodeFormat.QR_CODE; writer.Options.Width = texture.width; writer.Options.Height = texture.height; writer.Options.Margin = 10; PsicologoDAO psicologoDAO = new PsicologoDAO(); List <Psicologo> psicologos = psicologoDAO.Lista(); if (psicologos.Count > 0) { Psicologo psicologo = psicologos[0]; string representation = string.Format( "{0}\n{1}\n{2}\n{3}", psicologo.Nombre, psicologo.Correo, psicologo.Telefono, psicologo.Cedula ); BitMatrix matrix = writer.Encode(representation); matrix.rotate180(); ZXing.Common.BitArray row = new ZXing.Common.BitArray(matrix.RowSize); // get image data int width = texture.width; int height = texture.height; for (int y = 0; y < height; y++) { row = matrix.getRow(y, row); row.reverse(); // they are backwards wtf? int[] pixels = row.Array; int int_i = 0; int bit_i = 0; for (int x = 0; x < width; x++) { int bit_mask = 1 << bit_i++; int int_value = pixels[int_i]; bool bit_value = (int_value & bit_mask) == bit_mask; if (bit_i > 31) { bit_i = 0; int_i++; } UnityEngine.Color color; if (bit_value) { color = UnityEngine.Color.black; } else { color = UnityEngine.Color.white; } texture.SetPixel(x, y, color); } } texture.Apply(); image.texture = texture; //imageFitter.aspectRatio = 1.0f; //image.material.mainTexture = texture; } }
/// <summary> /// Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees /// </summary> public void rotate180() { var width = Width; var height = Height; var topRow = new BitArray(width); var bottomRow = new BitArray(width); for (int i = 0; i < (height + 1) / 2; i++) { topRow = getRow(i, topRow); bottomRow = getRow(height - 1 - i, bottomRow); topRow.reverse(); bottomRow.reverse(); setRow(i, bottomRow); setRow(height - 1 - i, topRow); } }