static VALUE rdmtx_decode(VALUE self, VALUE image /* Image from RMagick (Magick::Image) */, VALUE timeout /* Timeout in msec */) { VALUE rawImageString = rb_funcall(image, rb_intern("export_pixels_to_str"), 0); VALUE safeImageString = StringValue(rawImageString); char * imageBuffer = RSTRING_PTR(safeImageString); int width = NUM2INT(rb_funcall(image, rb_intern("columns"), 0)); int height = NUM2INT(rb_funcall(image, rb_intern("rows"), 0)); DmtxImage *dmtxImage = dmtxImageCreate((unsigned char *)imageBuffer, width, height, DmtxPack24bppRGB); VALUE results = rb_ary_new(); /* Initialize decode struct for newly loaded image */ DmtxDecode * decode = dmtxDecodeCreate(dmtxImage, 1); DmtxRegion * region; DmtxMessage * message; int intTimeout = NUM2INT(timeout); DmtxTime dmtxTimeout = dmtxTimeAdd(dmtxTimeNow(), intTimeout); for(;;) { if (intTimeout == 0) { region = dmtxRegionFindNext(decode, NULL); } else { region = dmtxRegionFindNext(decode, &dmtxTimeout); } if (region == NULL ) break; message = dmtxDecodeMatrixRegion(decode, region, DmtxUndefined); if (message != NULL) { VALUE outputString = rb_str_new2((char *)message->output); rb_ary_push(results, outputString); dmtxMessageDestroy(&message); } dmtxRegionDestroy(®ion); } dmtxDecodeDestroy(&decode); dmtxImageDestroy(&dmtxImage); return results; }
static PyObject * dmtx_decode(PyObject *self, PyObject *arglist, PyObject *kwargs) { int count=0; int found=0; int width; int height; int gap_size = DmtxUndefined; int max_count = DmtxUndefined; int timeout = DmtxUndefined; int shape = DmtxUndefined; int deviation = DmtxUndefined; int threshold = DmtxUndefined; int shrink = 1; int corrections = DmtxUndefined; int min_edge = DmtxUndefined; int max_edge = DmtxUndefined; PyObject *dataBuf = NULL; Py_ssize_t dataLen; PyObject *context = Py_None; PyObject *output = PyList_New(0); DmtxTime dmtx_timeout; DmtxImage *img; DmtxDecode *dec; DmtxRegion *reg; DmtxMessage *msg; DmtxVector2 p00, p10, p11, p01; const char *pxl; /* Input image buffer */ static char *kwlist[] = { "width", "height", "data", "gap_size", "max_count", "context", "timeout", "shape", "deviation", "threshold", "shrink", "corrections", "min_edge", "max_edge", NULL }; /* Parse out the options which are applicable */ PyObject *filtered_kwargs; filtered_kwargs = PyDict_New(); count = 3; /* Skip the first 3 keywords as they are sent in arglist */ while(kwlist[count]){ if(PyDict_GetItemString(kwargs, kwlist[count])) { PyDict_SetItemString(filtered_kwargs, kwlist[count], PyDict_GetItemString(kwargs, kwlist[count])); } count++; } /* Get parameters from Python for libdmtx */ if(!PyArg_ParseTupleAndKeywords(arglist, filtered_kwargs, "iiOi|iOiiiiiiii", kwlist, &width, &height, &dataBuf, &gap_size, &max_count, &context, &timeout, &shape, &deviation, &threshold, &shrink, &corrections, &min_edge, &max_edge)) { PyErr_SetString(PyExc_TypeError, "decode takes at least 3 arguments"); return NULL; } Py_INCREF(context); /* Reset timeout for each new page */ if(timeout != DmtxUndefined) dmtx_timeout = dmtxTimeAdd(dmtxTimeNow(), timeout); if(dataBuf == NULL) { PyErr_SetString(PyExc_TypeError, "Interleaved bitmapped data in buffer missing"); return NULL; } PyObject_AsCharBuffer(dataBuf, &pxl, &dataLen); img = dmtxImageCreate((unsigned char *)pxl, width, height, DmtxPack24bppRGB); if(img == NULL) return NULL; dec = dmtxDecodeCreate(img, shrink); if(dec == NULL) { dmtxImageDestroy(&img); return NULL; } if(gap_size != DmtxUndefined) dmtxDecodeSetProp(dec, DmtxPropScanGap, gap_size); if(shape != DmtxUndefined) dmtxDecodeSetProp(dec, DmtxPropSymbolSize, shape); if(deviation != DmtxUndefined) dmtxDecodeSetProp(dec, DmtxPropSquareDevn, deviation); if(threshold != DmtxUndefined) dmtxDecodeSetProp(dec, DmtxPropEdgeThresh, threshold); if(min_edge != DmtxUndefined) dmtxDecodeSetProp(dec, DmtxPropEdgeMin, min_edge); if(max_edge != DmtxUndefined) dmtxDecodeSetProp(dec, DmtxPropEdgeMax, max_edge); for(count=1; ;count++) { Py_BEGIN_ALLOW_THREADS if(timeout == DmtxUndefined) reg = dmtxRegionFindNext(dec, NULL); else reg = dmtxRegionFindNext(dec, &dmtx_timeout); Py_END_ALLOW_THREADS /* Finished file or ran out of time before finding another region */ if(reg == NULL) break; msg = dmtxDecodeMatrixRegion(dec, reg, corrections); if(msg != NULL) { p00.X = p00.Y = p10.Y = p01.X = 0.0; p10.X = p01.Y = p11.X = p11.Y = 1.0; dmtxMatrix3VMultiplyBy(&p00, reg->fit2raw); dmtxMatrix3VMultiplyBy(&p10, reg->fit2raw); dmtxMatrix3VMultiplyBy(&p11, reg->fit2raw); dmtxMatrix3VMultiplyBy(&p01, reg->fit2raw); PyList_Append(output, Py_BuildValue("s#((ii)(ii)(ii)(ii))", msg->output, msg->outputIdx, (int)((shrink * p00.X) + 0.5), height - 1 - (int)((shrink * p00.Y) + 0.5), (int)((shrink * p10.X) + 0.5), height - 1 - (int)((shrink * p10.Y) + 0.5), (int)((shrink * p11.X) + 0.5), height - 1 - (int)((shrink * p11.Y) + 0.5), (int)((shrink * p01.X) + 0.5), height - 1 - (int)((shrink * p01.Y) + 0.5))); Py_INCREF(output); dmtxMessageDestroy(&msg); found++; } dmtxRegionDestroy(®); /* Stop if we've reached maximium count */ if(max_count != DmtxUndefined) if(found >= max_count) break; } dmtxDecodeDestroy(&dec); dmtxImageDestroy(&img); Py_DECREF(context); if(output == NULL) { Py_INCREF(Py_None); return Py_None; } return output; }
/** * Decode the image, returning tags found (as DMTXTag objects) */ JNIEXPORT jobjectArray JNICALL Java_org_libdmtx_DMTXImage_getTags(JNIEnv *aEnv, jobject aImage, jint aTagCount, jint lSearchTimeout) { jclass lImageClass, lTagClass, lPointClass; jmethodID lTagConstructor, lPointConstructor; jfieldID lWidth, lHeight, lData; DmtxImage *lImage; DmtxDecode *lDecode; DmtxRegion *lRegion; DmtxTime lTimeout; int lW, lH, lI; jintArray lJavaData; jint *lPixels; jobject *lTags; jobjectArray lResult; int lTagCount = 0; /* Find DMTXImage class */ lImageClass = (*aEnv)->FindClass(aEnv, "org/libdmtx/DMTXImage"); if(lImageClass == NULL) return NULL; /* Find Tag class */ lTagClass = (*aEnv)->FindClass(aEnv, "org/libdmtx/DMTXTag"); if(lTagClass == NULL) return NULL; /* Find Point class */ lPointClass = (*aEnv)->FindClass(aEnv, "java/awt/Point"); if(lPointClass == NULL) return NULL; /* Find constructors */ lTagConstructor = (*aEnv)->GetMethodID( aEnv, lTagClass, "<init>", "(Ljava/lang/String;Ljava/awt/Point;Ljava/awt/Point;Ljava/awt/Point;Ljava/awt/Point;)V" ); lPointConstructor = (*aEnv)->GetMethodID(aEnv, lPointClass, "<init>", "(II)V"); if(lTagConstructor == NULL || lPointConstructor == NULL) return NULL; /* Find fields */ lWidth = (*aEnv)->GetFieldID(aEnv, lImageClass, "width", "I"); lHeight = (*aEnv)->GetFieldID(aEnv, lImageClass, "height", "I"); lData = (*aEnv)->GetFieldID(aEnv, lImageClass, "data", "[I"); if(lWidth == NULL || lHeight == NULL || lData == NULL) return NULL; /* Get fields */ lW = (*aEnv)->GetIntField(aEnv, aImage, lWidth); lH = (*aEnv)->GetIntField(aEnv, aImage, lHeight); lJavaData = (*aEnv)->GetObjectField(aEnv, aImage, lData); lPixels = (*aEnv)->GetIntArrayElements(aEnv, lJavaData, NULL); /* Create DmtxImage object */ lImage = dmtxImageCreate((unsigned char *)lPixels, lW, lH, DmtxPack32bppRGBX); if(lImage == NULL) return NULL; /* Create DmtxDecode object */ lDecode = dmtxDecodeCreate(lImage, 1); if(lDecode == NULL) return NULL; /* Allocate temporary Tag array */ lTags = (jobject *)malloc(aTagCount * sizeof(jobject)); if(lTags == NULL) return NULL; /* Find all tags that we can inside timeout */ lTimeout = dmtxTimeAdd(dmtxTimeNow(), lSearchTimeout); while(lTagCount < aTagCount && (lRegion = dmtxRegionFindNext(lDecode, &lTimeout))) { jstring sStringID; DmtxMessage *lMessage = dmtxDecodeMatrixRegion(lDecode, lRegion, DmtxUndefined); if(lMessage != NULL) { DmtxVector2 lCorner1, lCorner2, lCorner3, lCorner4; jobject lJCorner1, lJCorner2, lJCorner3, lJCorner4; /* Calculate position of Tag */ lCorner1.X = lCorner1.Y = lCorner2.Y = lCorner4.X = 0.0; lCorner2.X = lCorner4.Y = lCorner3.X = lCorner3.Y = 1.0; dmtxMatrix3VMultiplyBy(&lCorner1, lRegion->fit2raw); dmtxMatrix3VMultiplyBy(&lCorner2, lRegion->fit2raw); dmtxMatrix3VMultiplyBy(&lCorner3, lRegion->fit2raw); dmtxMatrix3VMultiplyBy(&lCorner4, lRegion->fit2raw); /* Create Location instances for corners */ lJCorner1 = (*aEnv)->NewObject(aEnv, lPointClass, lPointConstructor, (int) lCorner1.X, (int) (lH - lCorner1.Y - 1)); lJCorner2 = (*aEnv)->NewObject(aEnv, lPointClass, lPointConstructor, (int) lCorner2.X, (int) (lH - lCorner2.Y - 1)); lJCorner3 = (*aEnv)->NewObject(aEnv, lPointClass, lPointConstructor, (int) lCorner3.X, (int) (lH - lCorner3.Y - 1)); lJCorner4 = (*aEnv)->NewObject(aEnv, lPointClass, lPointConstructor, (int) lCorner4.X, (int) (lH - lCorner4.Y - 1)); /* Decode Message */ sStringID = (*aEnv)->NewStringUTF(aEnv, lMessage->output); /* Create Tag instance */ lTags[lTagCount] = (*aEnv)->NewObject(aEnv, lTagClass, lTagConstructor, sStringID, lJCorner1, lJCorner2, lJCorner3, lJCorner4); if(lTags[lTagCount] == NULL) return NULL; /* Increment Count */ lTagCount++; /* Free Message */ dmtxMessageDestroy(&lMessage); } /* Free Region */ dmtxRegionDestroy(&lRegion); } /* Free DMTX Structures */ dmtxDecodeDestroy(&lDecode); dmtxImageDestroy(&lImage); /* Release Image Data */ (*aEnv)->ReleaseIntArrayElements(aEnv, lJavaData, lPixels, 0); /* Create result array */ lResult = (*aEnv)->NewObjectArray(aEnv, lTagCount, lTagClass, NULL); for(lI = 0; lI < lTagCount; lI++) { (*aEnv)->SetObjectArrayElement(aEnv, lResult, lI, lTags[lI]); } /* Free local references */ (*aEnv)->DeleteLocalRef(aEnv, lJavaData); (*aEnv)->DeleteLocalRef(aEnv, lImageClass); (*aEnv)->DeleteLocalRef(aEnv, lTagClass); (*aEnv)->DeleteLocalRef(aEnv, lPointClass); return lResult; }
void renderCodeDatamatrix(OROPage *page, const QRectF &qrect, const QString &qstr, ORBarcodeData * bc) { //5 pixel par carré //qreal pix = 5; //lecture du type de datamatrix QRegExp regex("[a-zA-Z]{10}_([0-9]{1,2})_([LCR]{1})"); regex.indexIn(bc->format); int type = regex.cap(1).toInt(); QString align = regex.cap(2); size_t width, height, bytesPerPixel; //pointer declaration unsigned char *pxl = NULL; DmtxEncode *enc = NULL; DmtxImage *img = NULL; ORORect *rect = NULL; int valeur = 0; /* 1) ENCODE a new Data Matrix barcode image (in memory only) */ enc = dmtxEncodeCreate(); //see DmtxSymbolSize in dmtx.h for more details enc->sizeIdxRequest = type; enc->marginSize = 0; //number of pixel for one square enc->moduleSize = 1; try { //assert(enc != NULL); dmtxEncodeDataMatrix(enc, qstr.size(), (unsigned char*)qstr.toStdString().c_str()); /* 2) COPY the new image data before releasing encoding memory */ width = dmtxImageGetProp(enc->image, DmtxPropWidth); height = dmtxImageGetProp(enc->image, DmtxPropHeight); bytesPerPixel = dmtxImageGetProp(enc->image, DmtxPropBytesPerPixel); if(width > 1000000000) { throw std::runtime_error("Code is to big for the Datamatrix"); } pxl = (unsigned char *)malloc(width * height * bytesPerPixel); //assert(pxl != NULL); memcpy(pxl, enc->image->pxl, width * height * bytesPerPixel); dmtxEncodeDestroy(&enc); /* 3) DECODE the Data Matrix barcode from the copied image */ img = dmtxImageCreate(pxl, width, height, DmtxPack24bppRGB); QPen pen(Qt::NoPen); QBrush brush(QColor("black")); qreal Xo = 0; qreal Yo = 0; //length of square qreal pas = 0; datamatrixGeometry(align,qrect,img,&Xo,&Yo,&pas); //draw the datamatrix for(int y = 0; y < img->height; y++) { for(int x = 0; x < img->width; x++) { dmtxImageGetPixelValue(img,x,y,0,&valeur); rect = new ORORect(bc); rect->setPen(pen); if(valeur == 0) { brush.setColor(Qt::black); rect->setBrush(brush); rect->setRect(QRectF( Xo + x*pas, Yo - y*pas, pas, pas)); rect->setRotationAxis(qrect.topLeft()); page->addPrimitive(rect); } } delete rect; } //memory cleanning free(pxl); dmtxEncodeDestroy(&enc); dmtxImageDestroy(&img); } catch(...) { //there is a problem with the datamatrix //RR is printed printRR(page,bc,qrect); //memory cleaning if(rect != NULL) { delete rect; } if(enc != NULL) { dmtxEncodeDestroy(&enc); } if(img != NULL) { dmtxImageDestroy(&img); } if(pxl!=NULL) { free(pxl); } } }
bool Detector::detect(cv::Mat& image, int timeout, unsigned int offsetx, unsigned int offsety){ bool detected = false; lines_.clear(); message_.clear(); polygon_.clear(); DmtxRegion *reg; DmtxDecode *dec; DmtxImage *img; DmtxMessage *msg; DmtxTime t; img = dmtxImageCreate(image.data, image.cols, image.rows, DmtxPack24bppRGB); //dmtxImageSetProp(img, DmtxPropImageFlip, DmtxFlipY); dec = dmtxDecodeCreate(img, 1); assert(dec != NULL); t = dmtxTimeAdd(dmtxTimeNow(), timeout); reg = dmtxRegionFindNext(dec, &t); if(reg != NULL) { int height; int dataWordLength; int rotateInt; double rotate; DmtxVector2 p00, p10, p11, p01; height = dmtxDecodeGetProp(dec, DmtxPropHeight); p00.X = p00.Y = p10.Y = p01.X = 0.0; p10.X = p01.Y = p11.X = p11.Y = 1.0; dmtxMatrix3VMultiplyBy(&p00, reg->fit2raw); dmtxMatrix3VMultiplyBy(&p10, reg->fit2raw); dmtxMatrix3VMultiplyBy(&p11, reg->fit2raw); dmtxMatrix3VMultiplyBy(&p01, reg->fit2raw); polygon_.push_back(cv::Point(p00.X + offsetx,image.rows-p00.Y + offsety)); polygon_.push_back(cv::Point(p10.X + offsetx,image.rows-p10.Y + offsety)); polygon_.push_back(cv::Point(p11.X + offsetx,image.rows-p11.Y + offsety)); polygon_.push_back(cv::Point(p01.X + offsetx,image.rows-p01.Y + offsety)); lines_.push_back( std::pair<cv::Point,cv::Point>( cv::Point(p00.X + offsetx,image.rows-p00.Y + offsety), cv::Point(p10.X + offsetx,image.rows-p10.Y + offsety) ) ); lines_.push_back( std::pair<cv::Point,cv::Point>( cv::Point(p10.X + offsetx,image.rows-p10.Y + offsety), cv::Point(p11.X + offsetx,image.rows-p11.Y + offsety) ) ); lines_.push_back( std::pair<cv::Point,cv::Point>( cv::Point(p11.X + offsetx,image.rows-p11.Y + offsety), cv::Point(p01.X + offsetx,image.rows-p01.Y + offsety) ) ); lines_.push_back( std::pair<cv::Point,cv::Point>( cv::Point(p01.X + offsetx,image.rows-p01.Y + offsety), cv::Point(p00.X + offsetx,image.rows-p00.Y + offsety) ) ); detected = true; msg = dmtxDecodeMatrixRegion(dec, reg, DmtxUndefined); if(msg != NULL) { message_ = (const char*)msg->output; dmtxMessageDestroy(&msg); } dmtxRegionDestroy(®); } dmtxDecodeDestroy(&dec); dmtxImageDestroy(&img); return detected; }
bool Marker_DMTX::findPattern(const sensor_msgs::Image &img, std::vector<SMarker> &res) { int count=0; DmtxImage *dimg = dmtxImageCreate((unsigned char*)&img.data[0], img.width, img.height, DmtxPack24bppRGB); ROS_ASSERT(dimg); DmtxDecode *dec = dmtxDecodeCreate(dimg, 1); ROS_ASSERT(dec); dmtxDecodeSetProp(dec, DmtxPropEdgeThresh, 1); DmtxRegion *reg; for(count=1; ;count++) { DmtxTime timeout = dmtxTimeAdd(dmtxTimeNow(), timeout_); reg = dmtxRegionFindNext(dec, &timeout); /* Finished file or ran out of time before finding another region */ if(reg == NULL) break; if (reg != NULL) { DmtxMessage *msg = dmtxDecodeMatrixRegion(dec, reg, DmtxUndefined); if (msg != NULL) { SMarker m; m.code_ = std::string((const char*)msg->output,msg->outputSize); m.format_ = "datamatrix"; DmtxVector2 p00, p10, p11, p01; p00.X = p00.Y = p10.Y = p01.X = 0.0; p10.X = p01.Y = p11.X = p11.Y = 1.0; dmtxMatrix3VMultiplyBy(&p00, reg->fit2raw); dmtxMatrix3VMultiplyBy(&p10, reg->fit2raw); dmtxMatrix3VMultiplyBy(&p11, reg->fit2raw); dmtxMatrix3VMultiplyBy(&p01, reg->fit2raw); Eigen::Vector2i v; v(0)=(int)((p01.X) + 0.5); v(1)=img.height - 1 - (int)((p01.Y) + 0.5); m.pts_.push_back(v); v(0)=(int)((p00.X) + 0.5); v(1)=img.height - 1 - (int)((p00.Y) + 0.5); m.pts_.push_back(v); v(0)=(int)((p11.X) + 0.5); v(1)=img.height - 1 - (int)((p11.Y) + 0.5); m.pts_.push_back(v); v(0)=(int)((p10.X) + 0.5); v(1)=img.height - 1 - (int)((p10.Y) + 0.5); m.pts_.push_back(v); res.push_back(m); dmtxMessageDestroy(&msg); } dmtxRegionDestroy(®); } } dmtxDecodeDestroy(&dec); dmtxImageDestroy(&dimg); return false; }
/** * \brief Convert message into Data Matrix image * \param enc * \param inputSize * \param inputString * \param sizeIdxRequest * \return DmtxPass | DmtxFail */ extern DmtxPassFail dmtxEncodeDataMatrix(DmtxEncode *enc, int inputSize, unsigned char *inputString) { int sizeIdx; int width, height, bitsPerPixel; unsigned char *pxl; DmtxByte outputStorage[4096]; DmtxByteList output = dmtxByteListBuild(outputStorage, sizeof(outputStorage)); DmtxByteList input = dmtxByteListBuild(inputString, inputSize); input.length = inputSize; /* Future: stream = StreamInit() ... */ /* Future: EncodeDataCodewords(&stream) ... */ /* Encode input string into data codewords */ sizeIdx = EncodeDataCodewords(&input, &output, enc->sizeIdxRequest, enc->scheme, enc->fnc1); if(sizeIdx == DmtxUndefined || output.length <= 0) return DmtxFail; /* EncodeDataCodewords() should have updated any auto sizeIdx to a real one */ assert(sizeIdx != DmtxSymbolSquareAuto && sizeIdx != DmtxSymbolRectAuto); /* XXX we can remove a lot of this redundant data */ enc->region.sizeIdx = sizeIdx; enc->region.symbolRows = dmtxGetSymbolAttribute(DmtxSymAttribSymbolRows, sizeIdx); enc->region.symbolCols = dmtxGetSymbolAttribute(DmtxSymAttribSymbolCols, sizeIdx); enc->region.mappingRows = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixRows, sizeIdx); enc->region.mappingCols = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixCols, sizeIdx); /* Allocate memory for message and array */ enc->message = dmtxMessageCreate(sizeIdx, DmtxFormatMatrix); enc->message->padCount = 0; /* XXX this needs to be added back */ memcpy(enc->message->code, output.b, output.length); /* Generate error correction codewords */ RsEncode(enc->message, enc->region.sizeIdx); /* Module placement in region */ ModulePlacementEcc200(enc->message->array, enc->message->code, enc->region.sizeIdx, DmtxModuleOnRGB); width = 2 * enc->marginSize + (enc->region.symbolCols * enc->moduleSize); height = 2 * enc->marginSize + (enc->region.symbolRows * enc->moduleSize); bitsPerPixel = GetBitsPerPixel(enc->pixelPacking); if(bitsPerPixel == DmtxUndefined) return DmtxFail; assert(bitsPerPixel % 8 == 0); /* Allocate memory for the image to be generated */ pxl = (unsigned char *)malloc((width * bitsPerPixel / 8 + enc->rowPadBytes) * height); if(pxl == NULL) { perror("pixel malloc error"); return DmtxFail; } enc->image = dmtxImageCreate(pxl, width, height, enc->pixelPacking); if(enc->image == NULL) { perror("image malloc error"); return DmtxFail; } dmtxImageSetProp(enc->image, DmtxPropImageFlip, enc->imageFlip); dmtxImageSetProp(enc->image, DmtxPropRowPadBytes, enc->rowPadBytes); /* Insert finder and aligment pattern modules */ PrintPattern(enc); return DmtxPass; }