如题

解决方案 »

  1.   

    procedure AnalyzeBlock(Bmp:TBitMap);
    var
      P1:PByteArray;
      i,j,j1:Integer;
      preSx,startx,starty,endx,endy,preL,length,jiange,lines:integer;
      b:Byte;
    Begin
         Form2.ListBox2.Clear;
         startx:=-1;//黑块横坐标
         starty:=-1;//黑块纵坐标
         endx:=-1;
         length:=0;//黑块长度
         jiange:=0;//黑点间隔,用于判断黑点是否干扰点
         lines:=0;//黑块总数
         for i:=0 to Bmp.Height-1 do
            begin
                P1:=Bmp.ScanLine[i];
                j1:=0;
                startx:=-1;
                endx:=-1;
                length:=0;
                jiange:=0;
                for j:=0 to Bmp.Width-1 do
                   begin
                      //寻找第一个黑点
                      if (P1[j1]=0) and (startx=-1) then
                         begin
                              startx:=j;
                              jiange:=0;
                              Inc(j1,3);
                              continue;
                         end;
                      //计算黑点的长度
                      if (P1[j1]=0) and (startx<>-1) then
                         begin
                              length:=length+1;
                              endx:=j;
                              jiange:=0;
                              Inc(j1,3);
                              continue;
                         end;
                      //计算白点长度
                      if (P1[j1]=255) and (startx<>-1) then
                         begin
                              jiange:=jiange+1;
                         end;                  if jiange>9 then  
                         begin  
                              if length>18 then //找到了一个黑块
                                  begin                                  break;
                                  end
                              else
                                  begin     //干扰黑点
                                      startx:=-1;
                                      starty:=-1;
                                      endx:=-1;
                                      endy:=-1;
                                      length:=0;
                                      jiange:=0;
                                  end;
                         end;
                      Inc(j1,3);
                   end;
                
                if (length>18)and((abs(preL-length)>18) or (abs(preSx-startx)>50)) then
                     begin //增加黑块
                         Form2.ListBox2.Items.Add('('+inttostr(startx)+','+inttostr(endx)+','+inttostr(i)+')'+inttostr(length));
                         lines:=lines+1;
                     end;
                preSx:=startx;preL:=length;
            end;
            Form2.ListBox2.Items.Add(inttostr(lines)+'行');
    end;