Jump to content
someoneyoumightknow

Issue with Camera component setup and permissions

Recommended Posts

i am making an android app that uses ocr in order to read an id and get information however when trying to request permissions then activate the camera component it gives me an error stating  java.lang.runtimeexecption setparameters failed i understand this means there was an issue with setting up the properties of the camera but i dont understand why this is happening

 

 

this is the on click code for showing the panel that operates the camera component

procedure TProfilePage.frontImageIconClick(Sender: TObject);
begin
  if PermissionsService.IsPermissionGranted('android.permission.CAMERA') then
  begin
    try
      if profilePicBool then
        CameraComponent1.Kind := TCameraKind.FrontCamera
      else
        CameraComponent1.Kind := TCameraKind.BackCamera;

      CameraComponent1.Kind := TCameraKind.BackCamera;
      CameraComponent1.Quality := TVideoCaptureQuality.PhotoQuality;
      CameraComponent1.FocusMode := TFocusMode.ContinuousAutoFocus;
      rectangleOverlay.Visible := true;
      titleLabel.Text := 'Front Image';
      frontImageBool := true;
      profilePagePanel.Enabled := false;
      CameraPanel.Visible := true;
      CameraComponent1.Active := true;

    except
      on E: Exception do
        ShowMessage('Exception: ' + E.Message);
    end;
  end
  else
  begin
    PermissionsService.RequestPermissions(['android.permission.CAMERA'], RequestPermissionResult, RationaleProcedure);
  end;
end;

this is the requestpermissionresult procedure

procedure TProfilePage.RequestPermissionResult(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);
begin

  begin
    if (Length(AGrantResults) > 0) and (AGrantResults[0] = TPermissionStatus.Granted) then
    begin
      if TPlatformServices.Current.SupportsPlatformService(IFMXCameraService, Service) then
      begin
        CameraPanel.Visible := true;
        profilePagePanel.Enabled := false;

        CameraComponent1.Active := true;
      end
      else
        ShowMessage('Camera service not supported on this device.');
    end
    else
    begin
      ShowMessage('Camera permission denied.');
    end;
  end;
end;

the rationale procedure

procedure TProfilePage.RationaleProcedure(Sender: TObject; const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);
begin
  ShowMessage('Camera is needed for app functionality');
end;

and this is what is supposed to take the image in the panel with the camera component. I understand the code for this is probably horrendous but please note i am rather new at programming and dont have a lot of knowledge in delphi and fmx or android development.
 

procedure TProfilePage.CaptureCircleBtnInnerClick(Sender: TObject);
var
  ParsedJSON: TJSONObject;
  ImageType: integer;
begin
  if frontImageBool then
  begin
    CropImage(imagePreview, rectangleOverlay, frontImage);
    ImageType := 0;
    frontImageBool := false;
  end
  else if backImageBool then
  begin
    CropImage(imagePreview, rectangleOverlay, backImage);
    ImageType := 1;
    backImageBool := false;
  end
  else if profilePicBool then
  begin
    profilePicImage.MultiResBitmap := imagePreview.MultiResBitmap;
    profilePicBool := false;
  end
  else if tradingLicnsImgBool then
  begin
    tradingLicnsImage.MultiResBitmap := imagePreview.MultiResBitmap;
    tradingLicnsImgBool := false;
  end;

  if frontImage.Bitmap.IsEmpty then
  begin
    ShowMessage('Front image is empty.');
    CameraComponent1.Active := false;
    profilePagePanel.Enabled := true;
    cameraPanelLoader.Visible := false;
    Exit;
  end;

  response := MainPage.OCR(frontImage.Bitmap, ImageType); // This is the long-running part
  ShowMessage(response.StatusText);
  ShowMessage(response.ContentAsString());
  ParsedJSON := TJSONObject(TJSONObject.ParseJSONValue(response.ContentAsString(TEncoding.UTF8))).GetValue<TJSONObject>('structured_data');

  TTask.Run(
    procedure
    // var
    // response: IHTTPResponse;
    begin

      TThread.Queue(nil,
        procedure
        begin
          CameraPanel.Visible := false;
          profilePagePanel.Enabled := false;
          cameraPanelLoader.Visible := true;
        end);
      try
        TThread.Queue(nil,
          procedure
          begin
            case ImageType of
              0:
                begin
                  if Assigned(ParsedJSON) then
                  begin
                    userDataJSON := ParsedJSON;
                    firstNameEdit.Text := ParsedJSON.GetValue<string>('first_name_en');
                    lastNameEdit.Text := ParsedJSON.GetValue<string>('last_name_en');
                    mothersNameEdit.Text := ParsedJSON.GetValue<string>('mother_name_en');
                    fatherNameEdit.Text := ParsedJSON.GetValue<string>('father_name_en');
                    dateOfBirthEdit.Text := ParsedJSON.GetValue<string>('dob');
                    idNumberEdit.Text := ParsedJSON.GetValue<string>('id_number');
                  end
                  else
                    ShowMessage('Failed to parse structured_data.');
                end;
              1:
                begin
                  if Assigned(ParsedJSON) then
                  begin
                    userDataJSON := ParsedJSON;
                    recordNumberEdit.Text := ParsedJSON.GetValue<string>('registration_number_en');
                    // lastNameEdit.Text := ParsedJSON.GetValue<string>('last_name_en');
                    districtEdit.Text := ParsedJSON.GetValue<string>('district_en');
                    governorateEdit.Text := ParsedJSON.GetValue<string>('governorate_en');
                    dateOfBirthEdit.Text := ParsedJSON.GetValue<string>('dob');
                    idNumberEdit.Text := ParsedJSON.GetValue<string>('id_number');
                  end
                  else
                    ShowMessage('Failed to parse structured_data.');
                end;
            end;

          end);
      except
        on E: Exception do
          TThread.Queue(nil,
            procedure
            begin
              ShowMessage('OCR failed: ' + E.Message);
              CameraComponent1.Active := false;
              profilePagePanel.Enabled := true;
              cameraPanelLoader.Visible := false;
            end);
      end;

      TThread.Queue(nil,
        procedure
        begin
          CameraComponent1.Active := false;
          profilePagePanel.Enabled := true;
          cameraPanelLoader.Visible := false;
        end);
    end);
  CameraComponent1.Active := false;
  profilePagePanel.Enabled := true;
  cameraPanelLoader.Visible := false;
end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×