![](/img/trans.png)
[英]MediaRecorder start failed: -16 when change CamcorderProfile.QUALITY_LOW to CamcorderProfile.QUALITY_HIGH
[英]Firemonkey TCameraComponent quality change when reactivated
我正在使用適用於 Android 的 firemonkey 在 Delphi 10.1 Berlin 中構建條形碼閱讀器應用程序。 基於CameraComponent 示例並使用ZXing 庫,可以讀取條碼。
要初始化相機,我使用以下代碼:
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 提供的鏈接中實現相機優化技術。 它極大地加快了相機的幀速率。
有一點,我認為更好的掃描策略可以在連續任務中運行圖像掃描進度。
這是如何做到的:
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;
它真的很快。
總之,希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.