我正在使用Android的firemonkey在Delphi 10.1 Berlin中构建条形码读取器应用程序。基于CameraComponent sample并使用ZXing library,可以读取条形码。

要初始化相机,我使用以下代码:

procedure TfrmMain.btnOpenReaderClick(Sender: TObject);
begin
  CameraComponent.Active := False;
  CameraComponent.FocusMode := FMX.Media.TFocusMode.ContinuousAutoFocus;
  CameraComponent.Quality := TVideoCaptureQuality.MediumQuality;
  CameraComponent.Active := True;
  CameraComponent.SampleBufferToBitmap(imgCamera.Bitmap, True);
end;


要扫描条形码,我正在运行以下代码:

procedure TfrmMain.GetImage;
var
  ReadResult: TReadResult;
begin
  CameraComponent.SampleBufferToBitmap(imgCamera.Bitmap, True);

  if (FScanInProgress) then
    Exit;

  { This code will take every 4 frames. }
  inc(FFrameTake);
  if (FFrameTake mod 4 <> 0) then
    Exit;

  ReadResult := nil;

  ITask(TTask.Create(
    procedure
    begin
      try
        FScanInProgress := True;

        ReadResult := FScanManager.Scan(imgCamera.Bitmap);

        TThread.Synchronize(nil,
          procedure
          begin
          try
            if (ReadResult <> nil) then
            begin
              Label1.Text := ReadResult.text;
              CameraComponent.Active := False;
            end;
          except
            on E: Exception do
              ShowMessage(E.Message);
          end;
        end);
      finally
        ReadResult.Free;
        imgCamera.Bitmap.Free;
        FScanInProgress := false;
      end;
    end)).Start;
end;


读取条形码后,当我将CameraComponent.Active := True;设置为开始读取新条形码时,即使启动组件时将该属性设置为中等质量,CameraComponent的质量也会自动设置为高质量。这会导致相机预览以低帧频显示。重新激活CameraComponent时,是否可以将默认捕获设置设置为中?

最佳答案

是的,您必须在激活相机之前对其进行设置。喜欢:

CameraComponent1.Quality := TVideoCaptureQuality.MediumQuality;
CameraComponent1.Active := true;


顺便说一句:我只停止在Android上使用相机,而不是使用IOS。太慢了在应用程序停止时,我将使用IOS关闭相机。画布不再更新了。

procedure TdmGetBarcodeStatus.StopCamera();
begin
    CameraIsActivated := false;
{$IFDEF ANDROID}
    CameraComponent1.Active := false;
{$ENDIF}
end;


还要在Dave提供的链接中实施相机优化技术。它极大地提高了相机的帧速率。

有一点,我认为更好的扫描策略可以在连续Task中运行图像扫描进度。

这是可以做到的:

FParseImagesInProgress是一个标志,用于控制对来自TRectangle(RectImage.Fill.Bitmap.Bitmap)的图像的解析。在停止相机之前,请将FParseImagesInProgress设置为false。

procedure TFormCamera.StartParseImageTaskService();
var
    ReadResult: TReadResult;
begin

    if FParseImagesInProgress then
        Exit;

    FParseImagesInProgress := true;

    TTask.Run(
        procedure
        var
            hints: TDictionary<TDecodeHintType, TObject>;
            PossibleFormats: TList<TBarcodeFormat>;
            ScanManager: TScanManager;
            scanBitmap: TBitmap;
        begin
            PossibleFormats := TList<TBarcodeFormat>.Create();
            PossibleFormats.Add(TBarcodeFormat.QR_CODE);

            hints := TDictionary<TDecodeHintType, TObject>.Create();
            hints.Add(TDecodeHintType.POSSIBLE_FORMATS, PossibleFormats);

            ScanManager := TScanManager.Create(TBarcodeFormat.CODE_128, hints);
            scanBitmap := TBitmap.Create();

            try

                while (FParseImagesInProgress) do
                begin

                    ReadResult := nil;

                    try

                        TThread.Synchronize(nil,
                            procedure
                            begin
                                scanBitmap.Assign(RectImage.Fill.Bitmap.Bitmap);
                            end);

                        ReadResult := ScanManager.Scan(scanBitmap);

                    except
                        if Assigned(ReadResult) then
                            FreeAndNil(ReadResult);
                    end;

                    if Assigned(ReadResult) then
                    begin
                        TThread.Synchronize(nil,
                            procedure
                            begin
                                // PlaySound(TATSounds.Good);
                                MarkBarcode(ReadResult, TalphaColors.Deepskyblue);

                                if WasNotLastBarcodeInTimeWindow(ReadResult.Text) then
                                    FBarcodeRequestManager.RequestBarcodeStatus(ReadResult.Text, HotMember, 'myDevice', HotUser,
                                        HotPassword);

                                FLastBarcode := ReadResult.Text;

                            end);

                        FreeAndNil(ReadResult);
                    end;

                    Sleep(MS_BETWEEN_SCAN_FRAMES);

                end; // while

            finally

                if Assigned(scanBitmap) then
                    scanBitmap := nil;

                FreeAndNil(ScanManager);

                if Assigned(PossibleFormats) then
                begin
                    PossibleFormats.Clear;
                    PossibleFormats := nil;
                end;

                if Assigned(ReadResult) then
                    FreeAndNil(ReadResult);

            end;

        end); // end TTask

end;


真的很快。

无论如何,希望对您有所帮助!

10-05 22:13