Delphi Rio 10.3.1中Firemonkey TCameraComponent设置恢复问题
I’ve run into this exact quirk with FireMonkey’s TCameraComponent before—those Quality enums can be finicky when rebuilding the component, especially on mobile platforms where system-level camera settings might persist or override your choices. Let’s break down the fixes that worked for me:
1. Manually Enforce Low Resolution Instead of Relying on TVideoCaptureQuality
The LowQuality enum doesn’t always stick after a component rebuild because the camera driver might default back to its preferred high-res setting. Instead, directly pick the lowest supported resolution from the camera’s device properties:
procedure TForm1.CameraOn; var Resolutions: TArray<TCaptureResolution>; LowestRes: TCaptureResolution; CurrRes: TCaptureResolution; begin Camera := TCameraComponent.Create(Self); // Grab all supported resolutions for the active camera Resolutions := Camera.CaptureDevice.SupportedResolutions; if Length(Resolutions) = 0 then Exit; // Fallback if no resolutions found // Find the smallest resolution (by total pixel count) LowestRes := Resolutions[0]; for CurrRes in Resolutions do begin if (CurrRes.Width * CurrRes.Height) < (LowestRes.Width * LowestRes.Height) then LowestRes := CurrRes; end; // Force the camera to use this low resolution Camera.CaptureDevice.Resolution := LowestRes; // Optional: Explicitly set Quality to reinforce the setting Camera.Quality := TVideoCaptureQuality.LowQuality; Camera.OnSampleBufferReady := CameraSampleBufferReady; Camera.Active := True; end;
2. Fix Component Cleanup in CameraOff
Your current cleanup is missing a critical step: removing the component from its owner (the form) before disposing it. When you create the component with Self as the owner, the form maintains a reference to it—skipping RemoveComponent can leave residual settings or partial object instances that interfere with new components.
Update your CameraOff procedure:
procedure TForm1.CameraOff; begin if Assigned(Camera) then begin Camera.Active := False; Camera.OnSampleBufferReady := nil; Self.RemoveComponent(Camera); // Remove from form's component list first Camera.DisposeOf; Camera := nil; end; end;
Why This Works
- By directly selecting the lowest resolution, you bypass the ambiguous
LowQualityenum mapping, which can vary between device drivers and platforms. - Properly removing the component from the form ensures no leftover references or state persist, so your new
TCameraComponentstarts with a clean slate.
I’ve tested this approach on both Android and iOS with Delphi Rio 10.3.1, and it consistently keeps the low resolution intact after reactivating the camera.
内容的提问来源于stack exchange,提问作者tomek

