This code defines the class 'ImageProcesTwoValue', which is a subclass of 'ImageProcesService'. 'ImageProcesTwoValue' implements the 'ImageProces' method, which takes a pointer to image data as input and returns the size of the image data after processing. The processing involves converting the image to a grayscale image and then converting it to a binary image with a threshold determined by the average pixel value of the grayscale image.

Additionally, the code defines the class 'ImageProcesTwoValueBundleActivator', which implements the 'BundleActivator' interface to start and stop the bundle. In the 'start' method, it registers the 'ImageProcesTwoValue' service with the service registry using the 'IMAGE_PROCES_SERVICE' identifier. In the 'stop' method, it unregisters the service.

class ImageProcesTwoValue : public ImageProcesService
{
public:
    ImageProcesTwoValue() {}
    ~ImageProcesTwoValue() {}
    unsigned long ImageProces(void *data)
    {
        BITMAPFILEHEADER bfhead;
        BITMAPINFOHEADER bihead;
        RGBQUAD *pColorTable;

        memcpy(&bfhead, data, 14);   //灏嗘枃浠跺ご璇诲叆鍐呭瓨
        memcpy(&bihead, data+14, 40);   //灏嗕俊鎭ご璇诲叆鍐呭瓨
        int LineByte=(bihead.biWidth*24/8+3)/4*4; //淇濊瘉姣忚瀛楄妭鏁颁负4鐨勬暣鏁板€�
        unsigned char *pBmpBuf=new unsigned char[LineByte*bihead.biHeight]; //涓烘暟鎹尯鍒嗛厤鍐呭瓨绌洪棿
        memcpy(pBmpBuf, data+54, LineByte*bihead.biHeight);       //灏哹mp鏁版嵁鍖鸿鍏ュ唴瀛�


        //鐜板皢鐪熷僵鍥剧伆搴﹀寲
        int LineByte1=(bihead.biWidth*8/8+3)/4*4;  //鐢变簬鐏板害鍖栧悗姣忓儚绱犱綅鏁板彉涓�8锛屾墍浠ユ瘡琛屽瓧鑺傛暟鍙戠敓鏀瑰彉锛屼絾浠嶈姹備负4鐨勬暣鏁板€�

        //鏇存敼鏂囦欢澶达紝骞跺皢鍏朵繚瀛�
        bfhead.bfSize=14+40+sizeof(RGBQUAD)*256+LineByte1*bihead.biHeight;   //鏇存敼鏂囦欢澶у皬
        bfhead.bfOffBits=14+40+sizeof(RGBQUAD)*256;   //鏇存敼鍋忕Щ鍊�
        memcpy(data, &bfhead, 14);

        //鏇存敼淇℃伅澶村苟灏嗗叾淇濆瓨
        bihead.biBitCount=8;    //鏇存敼姣忓儚绱犱綅鏁�
        bihead.biSizeImage=LineByte1*bihead.biHeight;  //鏇存敼鏁版嵁鍖哄ぇ灏�
        memcpy(data+14, &bihead,40);

        //鍥犱负鐏板害鍖栧浘鍍忔湁棰滆壊琛紝鎵€浠ュ垱寤洪鑹茶〃骞朵繚瀛�
        pColorTable=new RGBQUAD[256];
        for(int i=0;i<256;i++)
         pColorTable[i].rgbRed = pColorTable[i].rgbGreen = pColorTable[i].rgbBlue = i;//浣块鑹茶〃涓瘡绉嶉鑹茬殑R,G,B鍒嗛噺鐩哥瓑涓旂瓑浜庣储寮曞€�
        memcpy(data+54, pColorTable, sizeof(RGBQUAD)*256);

        //鏀瑰彉鏁版嵁鍖�
        unsigned char *pBmpBuf1 = new unsigned char[LineByte1*bihead.biHeight];
        for(int i=0;i<bihead.biHeight;i++)
         for(int j=0;j<bihead.biWidth;j++)
         {
          unsigned char *pb1,*pb2;
          pb1=pBmpBuf+i*LineByte+j*3;
          int y=*(pb1)*0.299+*(pb1+1)*0.587+*(pb1+2)*0.114;
          pb2=pBmpBuf1+i*LineByte1+j;
          *pb2=y;
         }
         //浜屽€煎寲鏂规硶涓€锛氶槇鍊艰涓�127锛岀伆搴﹀€煎皬浜�127鐨勭疆闆讹紝鍏朵粬鐨勭疆涓�255锛�

        //for(int i=0;i<bihead.biHeight;i++)
        // for(int j=0;j<bihead.biWidth;j++)
        // {
        //  unsigned char *pb;
        //  pb=pBmpBuf1+i*LineByte1+j;
        //  if(*pb<127)   //灏嗘瘡涓儚绱犲€间笌127姣旇緝
        //   *pb=0;
        //  else
        //   *pb=255;
        // }
        //鏂规硶浜�:璁$畻鍍忕礌鐨勫钩鍧囧€糑锛屾壂鎻忓浘鍍忕殑姣忎釜鍍忕礌鍊煎鍍忕礌鍊煎ぇ浜嶬鍍忕礌鍊艰涓�255(鐧借壊)锛屽€煎皬浜庣瓑浜嶬鍍忕礌鍊艰涓�0(榛戣壊)
        int y=0;//鍍忕礌鍜�
        int k=0;//鍍忕礌涓暟
        for(int i=0;i<bihead.biHeight;i++)
         for(int j=0;j<bihead.biWidth;j++)
         {
          unsigned char *pb;
          pb=pBmpBuf1+i*LineByte1+j;
          y=y+*pb;  //璁$畻鎵€鏈夊儚绱犵伆搴﹀€间箣鍜�
          k++;      //缁熻鍍忕礌涓暟
         }
        y=y/k; //姹傚儚绱犲钩鍧囧€�
        for(int i=0;i<bihead.biHeight;i++)
         for(int j=0;j<bihead.biWidth;j++)
         {
          unsigned char *pb1;
          pb1=pBmpBuf1+i*LineByte1+j;
          if(*pb1<y)         //灏嗘瘡涓儚绱犲€间笌骞冲潎鍊间綔姣旇緝
           *pb1=0;
          else
           *pb1=255;
         }

        memcpy(data+54+sizeof(RGBQUAD)*256, pBmpBuf1,LineByte1*bihead.biHeight);
        return (unsigned long)bfhead.bfSize;
    }

    const std::type_info& type() const
    {
        return typeid(ImageProcesService);
    }

    bool isA(const std::type_info& otherType) const
    {
        std::string name(typeid(ImageProcesService).name());
        return name == otherType.name() || TsingHua::TMPP::Service::isA(otherType);
    }
};

class ImageProcesTwoValueBundleActivator: public TsingHua::TMPP::BundleActivator
{
private:
    TsingHua::TMPP::ServiceRef::Ptr _pService;
    TsingHua::TMPP::Service::Ptr pService;

public:
    void start(TsingHua::TMPP::BundleContext::Ptr pContext)
    {
        if (pService.isNull()) {
            pService = new ImageProcesTwoValue();
        }
        _pService = pContext->registry().registerService(_IMAGE_PROCES_SERVICE_, pService, TsingHua::TMPP::Properties());
    }

    void stop(TsingHua::TMPP::BundleContext::Ptr pContext)
    {
        pContext->registry().unregisterService(_pService);
    }
};
Image Proces Two Value: Image Processing Service for Binary Conversion

原文地址: https://www.cveoy.top/t/topic/oCnR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录