| 1 | package datamatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/ |
| 2 | |
| 3 | import ( |
| 4 | "github.com/makiuchi-d/gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/" |
| 5 | "github.com/makiuchi-d/gozxing/datamatrix/encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/" |
| 6 | qrencoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ "github.com/makiuchi-d/gozxing/qrcode/encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/" |
| 7 | ) |
| 8 | |
| 9 | // DataMatrixWriter This object renders a Data Matrix code as a BitMatrix 2D array of greyscale values. |
| 10 | type DataMatrixWriterscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/DataMatrixWriter# struct{} |
| 11 | |
| 12 | func NewDataMatrixWriterscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/NewDataMatrixWriter().() gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.Writerscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/Writer# { |
| 13 | return &DataMatrixWriterscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/DataMatrixWriter#{} |
| 14 | } |
| 15 | |
| 16 | func (this *DataMatrixWriterscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/DataMatrixWriter#) EncodeWithoutHintscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/DataMatrixWriter#EncodeWithoutHint().( |
| 17 | contents string, format gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.BarcodeFormatscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BarcodeFormat#, width, height int) (*gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.BitMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BitMatrix#, error) { |
| 18 | return this.Encodescip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/DataMatrixWriter#Encode().(contents, format, width, height, nil) |
| 19 | } |
| 20 | |
| 21 | func (this *DataMatrixWriterscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/DataMatrixWriter#) Encodescip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/DataMatrixWriter#Encode().(contents string, format gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.BarcodeFormatscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BarcodeFormat#, |
| 22 | width, height int, hints map[gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.EncodeHintTypescip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/EncodeHintType#]interface{}) (*gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.BitMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BitMatrix#, error) { |
| 23 | |
| 24 | if contents == "" { |
| 25 | return nil, gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.NewWriterExceptionscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/NewWriterException().("IllegalArgumentException: Found empty contents") |
| 26 | } |
| 27 | |
| 28 | if format != gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.BarcodeFormat_DATA_MATRIXscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BarcodeFormat_DATA_MATRIX. { |
| 29 | return nil, gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.NewWriterExceptionscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/NewWriterException().( |
| 30 | "IllegalArgumentException: Can only encode DATA_MATRIX, but got %v", format) |
| 31 | } |
| 32 | |
| 33 | if width < 0 || height < 0 { |
| 34 | return nil, gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.NewWriterExceptionscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/NewWriterException().( |
| 35 | "IllegalArgumentException: Requested dimensions can't be negative: %vx%v", width, height) |
| 36 | } |
| 37 | |
| 38 | // Try to get force shape & min / max size |
| 39 | shape := encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/.SymbolShapeHint_FORCE_NONEscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolShapeHint_FORCE_NONE. |
| 40 | var minSize *gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.Dimensionscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/Dimension# |
| 41 | var maxSize *gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.Dimensionscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/Dimension# |
| 42 | if hints != nil { |
| 43 | if val, ok := hints[gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.EncodeHintType_DATA_MATRIX_SHAPEscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/EncodeHintType_DATA_MATRIX_SHAPE.]; ok { |
| 44 | if requestedShape, ok := val.(encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/.SymbolShapeHintscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolShapeHint#); ok { |
| 45 | shape = requestedShape |
| 46 | } |
| 47 | } |
| 48 | if val, ok := hints[gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.EncodeHintType_MIN_SIZEscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/EncodeHintType_MIN_SIZE.]; ok { |
| 49 | if requestedMinSize, ok := val.(*gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.Dimensionscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/Dimension#); ok { |
| 50 | minSize = requestedMinSize |
| 51 | } |
| 52 | } |
| 53 | if val, ok := hints[gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.EncodeHintType_MAX_SIZEscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/EncodeHintType_MAX_SIZE.]; ok { |
| 54 | if requestedMaxSize, ok := val.(*gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.Dimensionscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/Dimension#); ok { |
| 55 | maxSize = requestedMaxSize |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | //1. step: Data encodation |
| 61 | encoded, e := encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/.EncodeHighLevelscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/EncodeHighLevel().(contents, shape, minSize, maxSize) |
| 62 | if e != nil { |
| 63 | return nil, e |
| 64 | } |
| 65 | |
| 66 | symbolInfo, _ := encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/.SymbolInfo_Lookupscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo_Lookup().(len(encoded), shape, minSize, maxSize, true) |
| 67 | |
| 68 | //2. step: ECC generation |
| 69 | codewords, _ := encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/.ErrorCorrection_EncodeECC200scip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/ErrorCorrection_EncodeECC200().(encoded, symbolInfo) |
| 70 | |
| 71 | //3. step: Module placement in Matrix |
| 72 | placement := encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/.NewDefaultPlacementscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/NewDefaultPlacement().(codewords, |
| 73 | symbolInfo.GetSymbolDataWidthscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetSymbolDataWidth().(), symbolInfo.GetSymbolDataHeightscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetSymbolDataHeight().()) |
| 74 | placement.Placescip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/DefaultPlacement#Place().() |
| 75 | |
| 76 | //4. step: low-level encoding |
| 77 | return encodeLowLevelscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/encodeLowLevel().(placement, symbolInfo, width, height), nil |
| 78 | } |
| 79 | |
| 80 | // encodeLowLevel Encode the given symbol info to a bit matrix. |
| 81 | // |
| 82 | // @param placement The DataMatrix placement. |
| 83 | // @param symbolInfo The symbol info to encode. |
| 84 | // @return The bit matrix generated. |
| 85 | // |
| 86 | func encodeLowLevelscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/encodeLowLevel().(placement *encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/.DefaultPlacementscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/DefaultPlacement#, |
| 87 | symbolInfo *encoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/.SymbolInfoscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#, width, height int) *gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.BitMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BitMatrix# { |
| 88 | |
| 89 | symbolWidth := symbolInfo.GetSymbolDataWidthscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetSymbolDataWidth().() |
| 90 | symbolHeight := symbolInfo.GetSymbolDataHeightscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetSymbolDataHeight().() |
| 91 | |
| 92 | matrix := qrencoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/.NewByteMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/NewByteMatrix().(symbolInfo.GetSymbolWidthscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetSymbolWidth().(), symbolInfo.GetSymbolHeightscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetSymbolHeight().()) |
| 93 | |
| 94 | matrixY := 0 |
| 95 | |
| 96 | for y := 0; y < symbolHeight; y++ { |
| 97 | // Fill the top edge with alternate 0 / 1 |
| 98 | var matrixX int |
| 99 | if (y % symbolInfo.GetMatrixHeightscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetMatrixHeight().()) == 0 { |
| 100 | matrixX = 0 |
| 101 | for x := 0; x < symbolInfo.GetSymbolWidthscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetSymbolWidth().(); x++ { |
| 102 | matrix.SetBoolscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ByteMatrix#SetBool().(matrixX, matrixY, (x%2) == 0) |
| 103 | matrixX++ |
| 104 | } |
| 105 | matrixY++ |
| 106 | } |
| 107 | matrixX = 0 |
| 108 | for x := 0; x < symbolWidth; x++ { |
| 109 | // Fill the right edge with full 1 |
| 110 | if (x % symbolInfo.GetMatrixWidthscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetMatrixWidth().()) == 0 { |
| 111 | matrix.SetBoolscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ByteMatrix#SetBool().(matrixX, matrixY, true) |
| 112 | matrixX++ |
| 113 | } |
| 114 | matrix.SetBoolscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ByteMatrix#SetBool().(matrixX, matrixY, placement.GetBitscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/DefaultPlacement#GetBit().(x, y)) |
| 115 | matrixX++ |
| 116 | // Fill the right edge with alternate 0 / 1 |
| 117 | if (x % symbolInfo.GetMatrixWidthscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetMatrixWidth().()) == symbolInfo.GetMatrixWidthscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetMatrixWidth().()-1 { |
| 118 | matrix.SetBoolscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ByteMatrix#SetBool().(matrixX, matrixY, (y%2) == 0) |
| 119 | matrixX++ |
| 120 | } |
| 121 | } |
| 122 | matrixY++ |
| 123 | // Fill the bottom edge with full 1 |
| 124 | if (y % symbolInfo.GetMatrixHeightscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetMatrixHeight().()) == symbolInfo.GetMatrixHeightscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetMatrixHeight().()-1 { |
| 125 | matrixX = 0 |
| 126 | for x := 0; x < symbolInfo.GetSymbolWidthscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix/encoder`/SymbolInfo#GetSymbolWidth().(); x++ { |
| 127 | matrix.SetBoolscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ByteMatrix#SetBool().(matrixX, matrixY, true) |
| 128 | matrixX++ |
| 129 | } |
| 130 | matrixY++ |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return convertByteMatrixToBitMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/convertByteMatrixToBitMatrix().(matrix, width, height) |
| 135 | } |
| 136 | |
| 137 | // convertByteMatrixToBitMatrix Convert the ByteMatrix to BitMatrix. |
| 138 | // |
| 139 | // @param reqHeight The requested height of the image (in pixels) with the Datamatrix code |
| 140 | // @param reqWidth The requested width of the image (in pixels) with the Datamatrix code |
| 141 | // @param matrix The input matrix. |
| 142 | // @return The output matrix. |
| 143 | // |
| 144 | func convertByteMatrixToBitMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/datamatrix`/convertByteMatrixToBitMatrix().(matrix *qrencoderscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/.ByteMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ByteMatrix#, reqWidth, reqHeight int) *gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.BitMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BitMatrix# { |
| 145 | matrixWidth := matrix.GetWidthscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ByteMatrix#GetWidth().() |
| 146 | matrixHeight := matrix.GetHeightscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ByteMatrix#GetHeight().() |
| 147 | outputWidth := reqWidth |
| 148 | if outputWidth < matrixWidth { |
| 149 | outputWidth = matrixWidth |
| 150 | } |
| 151 | outputHeight := reqHeight |
| 152 | if outputHeight < matrixHeight { |
| 153 | outputHeight = matrixHeight |
| 154 | } |
| 155 | |
| 156 | multiple := outputWidth / matrixWidth |
| 157 | if mh := outputHeight / matrixHeight; mh < multiple { |
| 158 | multiple = mh |
| 159 | } |
| 160 | |
| 161 | leftPadding := (outputWidth - (matrixWidth * multiple)) / 2 |
| 162 | topPadding := (outputHeight - (matrixHeight * multiple)) / 2 |
| 163 | |
| 164 | var output *gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.BitMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BitMatrix# |
| 165 | |
| 166 | // remove padding if requested width and height are too small |
| 167 | if reqHeight < matrixHeight || reqWidth < matrixWidth { |
| 168 | leftPadding = 0 |
| 169 | topPadding = 0 |
| 170 | output, _ = gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.NewBitMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/NewBitMatrix().(matrixWidth, matrixHeight) |
| 171 | } else { |
| 172 | output, _ = gozxingscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/.NewBitMatrixscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/NewBitMatrix().(reqWidth, reqHeight) |
| 173 | } |
| 174 | |
| 175 | output.Clearscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BitMatrix#Clear().() |
| 176 | for inputY, outputY := 0, topPadding; inputY < matrixHeight; inputY, outputY = inputY+1, outputY+multiple { |
| 177 | // Write the contents of this row of the bytematrix |
| 178 | for inputX, outputX := 0, leftPadding; inputX < matrixWidth; inputX, outputX = inputX+1, outputX+multiple { |
| 179 | if matrix.Getscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing/qrcode/encoder`/ByteMatrix#Get().(inputX, inputY) == 1 { |
| 180 | output.SetRegionscip-go gomod github.com/makiuchi-d/gozxing . `github.com/makiuchi-d/gozxing`/BitMatrix#SetRegion().(outputX, outputY, multiple, multiple) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return output |
| 186 | } |