问题描述
我正在使用Android的firemonkey在Delphi 10.1 Berlin中构建条形码读取器应用程序.基于 CameraComponent示例,并使用 ZXing库,便可以读取条形码.
I'm building a barcode reader application in Delphi 10.1 Berlin with firemonkey for Android. Based on the CameraComponent sample and using the ZXing library, it was possible to read the barcode.
要初始化相机,请使用以下代码:
To initialize the camera, I'm using this code:
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;
要扫描条形码,我正在运行它:
To scan the barcode, I'm running this:
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时,是否可以将默认捕获设置设置为中"?
After reading the barcode, when I set CameraComponent.Active := True;
to start reading a new barcode, the CameraComponent quality is automatically set to high quality, even if the property is set as medium quality when starting the component. This causes the preview of the camera to show at low frame rate. Is there a way to set the default capture setting to medium when reactivating the CameraComponent?
推荐答案
是的,必须在激活摄像机之前对其进行设置.像:
Yes you have to setup the camera before you activate it. Like:
CameraComponent1.Quality := TVideoCaptureQuality.MediumQuality;
CameraComponent1.Active := true;
btw:我只能在Android上停止相机,而不能在IOS上停止.太慢了在应用程序停止时,我将使用IOS关闭相机.然后,画布将不再更新.
btw: I only stop the camera on Android and not with IOS. That's too slow. I'll close the camera with IOS on application stop. The canvas is not updated anymore then.
procedure TdmGetBarcodeStatus.StopCamera();
begin
CameraIsActivated := false;
{$IFDEF ANDROID}
CameraComponent1.Active := false;
{$ENDIF}
end;
还可以在Dave提供的链接中实施摄像头优化技术.它可以极大地提高摄像头帧速率.
Also implement the camera optimization technique in the link provided by Dave. Its speeds up the camera frame rate tremendously.
有一点,我认为更好的扫描策略是,您可以在连续的Task中运行图像扫描进度.
For a little, I think better scan strategy you can run the image scan progress in a continuous Task.
这是可以完成的方法:
FParseImagesInProgress是一个标志,用于控制对来自TRectangle(RectImage.Fill.Bitmap.Bitmap)的图像的解析.在停止相机之前,请将FParseImagesInProgress设置为false.
FParseImagesInProgress is a flag which controls the parsing of the images coming from a TRectangle (RectImage.Fill.Bitmap.Bitmap). Before you stop the camera you set FParseImagesInProgress to 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;
这真的很快.
无论如何,希望对您有所帮助!
Anyway, hope it helps!
这篇关于重新激活后,Firemonkey TCameraComponent的质量发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!