原来使用华为的SDK,但是升级到D12.1后,不能正常使用了,现在把它改成Zxing的条码和二维码识别库。
主要识别过程:
procedure TScanForm.ParseBitmap;
var
ReadResult: TReadResult;
ScanResult: TScanResult;
begin
ReadResult := nil;
Camera.SampleBufferToBitmap(imgCamera.Bitmap, True);
if not fScanInProgress and (MilliSecondsBetween(fLastScan, Now) >= SCAN_EACH_MS) then
begin
TTask.Run(procedure
var
LReducedBuffer: TBitmap;
begin
try
try
fLastScan := Now;
fScanInProgress := True;
LReducedBuffer := CropBitmap(imgCamera);
try
ReadResult := fScanManager.Scan(LReducedBuffer);
finally
LReducedBuffer.Free;
end;
TThread.Synchronize(nil, procedure
begin
if (ReadResult <> nil) then
begin
ScanResult := TScanResult.Create; //建立消息
ScanResult.Code := 100;
ScanResult.Value := ReadResult.Text;
TMessageManager.DefaultManager.SendMessage(nil, ScanResult);//发送消息
// comment the next block if you don't want/need the beep,
// or create a configuration and let the user to decide
fAudioM.PlaySound(0);
//close the form
Camera.Active := False;
Close;
end;
end);
except
on E: Exception do
begin
exit;
end;
end;
finally
ReadResult.Free;
fScanInProgress := False;
end;
end);
end;
end;
设置摄相头参数:
Camera.Kind := FMX.Media.TCameraKind.BackCamera;
Camera.FocusMode := FMX.Media.TFocusMode.ContinuousAutoFocus;
Camera.Active := True;
打开或关闭闪光灯:
if Camera.Active and Camera.HasTorch then
begin
if Camera.TorchMode = TTorchMode.ModeOn then
begin
Camera.TorchMode := TTorchMode.ModeOff;
imgLight.Opacity := 0.6;
end else
begin
Camera.TorchMode := TTorchMode.ModeOn;
imgLight.Opacity := 1;
end;
end;
页面效果图: