Oppure

Loading
03/06/20 14:43
dylan666
Allora, funzionicchia ma con un problema.
Il codice a cui (grazie a te) siamo arrivati è questo:

Imports Google.Api.Gax.ResourceNames
Imports Google.Cloud.Dlp.V2
Imports Google.Protobuf
Imports System.IO
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RedactFromImage("PROJECT-######", "C:\tmp\ID.jpg", "C:\tmp\ID3.jpg")
    End Sub

    Function RedactFromImage(ByVal projectId As String, ByVal originalImagePath As String, ByVal redactedImagePath As String) As Object
        Dim request = New RedactImageRequest
        request.ParentAsProjectName = New ProjectName(projectId)
        request.InspectConfig = New InspectConfig
        request.InspectConfig.MinLikelihood = Likelihood.Likely
        request.InspectConfig.Limits = New InspectConfig.Types.FindingLimits()
        request.InspectConfig.Limits.MaxFindingsPerItem = 5
        request.InspectConfig.IncludeQuote = True


        request.InspectConfig.InfoTypes.Add(New InfoType With {.Name = "PERSON_NAME"})
        'request.InspectConfig.InfoTypes.Add(New InfoType With {.Name = "EMAIL_ADDRESS"})

        request.ByteItem = New ByteContentItem
        request.ByteItem.Type = ByteContentItem.Types.BytesType.ImagePng
        request.ByteItem.Data = ByteString.FromStream(New FileStream(originalImagePath, FileMode.Open))

        Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "C:\tmp\PROJECT-######.json")
        Dim client As DlpServiceClient = DlpServiceClient.Create()
        Dim response = client.RedactImage(request)
        'Console.WriteLine($"Extracted text: {response.ExtractedText}")

        ' Writes redacted image into file
        response.RedactedImage.WriteTo(New FileStream(redactedImagePath, FileMode.Create, FileAccess.Write))
        Return 0

        MsgBox("Fatto!")

    End Function

End Class



Il problema è che quando clicco sul bottone il file di output chiamato "ID3.jpg" viene creato, ma resta di 0KB e il MsgBox che ho messo alla fine non appare mai.
Appena termino l'esecuzione con "Stop debugging" del Visual Studio il file diventa di un peso "normale" e risulta correttamente elaborato.
Ma allora perché prima sembra rimasto "appeso" e il MsgBox non viene mostrato?
Con il listato in C# questo non accade.

Grazie
aaa
03/06/20 17:54
Carlo
il file, se lo vuoi leggere prima della fine del programma, deve essere chiuso, sia in VB che in C#.

Per chiudere il file immediatamente dopo la creazione e renderne possibile l'accesso, sostituisci:
' Writes redacted image into file
response.RedactedImage.WriteTo(New FileStream(redactedImagePath, FileMode.Create, FileAccess.Write))


con:
' Writes redacted image into file
Dim fs As FileStream = File.Create(redactedImagePath, FileAccess.Write)
response.RedactedImage.WriteTo(fs)
fs.Close()


l'MSGBOX dopo il return non apparirà mai! :asd:


Ultima modifica effettuata da Carlo 03/06/20 18:52
in programmazione tutto è permesso
04/06/20 10:26
Carlo
Il codice di esempio in C# è per usare le API, non è un buon esempio di programmazione.
Anche il file di origine "ID.png" non viene mai chiuso, è giusto lasciarlo "bloccato" durante l'elaborazione per evitarne la modifica, ma al termine deve essere chiuso insieme al file di destinazione "ID3.png".
Posto solo la parte finale della funzione che esegue correttamente l'apertura e la chiusura dei files:
request.ByteItem = New ByteContentItem
request.ByteItem.Type = ByteContentItem.Types.BytesType.ImagePng
Dim fsRe As FileStream = File.Open(originalImagePath, FileMode.Open, FileAccess.Read)
request.ByteItem.Data = ByteString.FromStream(fsRe)

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "C:\tmp\PROJECT-######.json")
Dim client As DlpServiceClient = DlpServiceClient.Create()
Dim response = client.RedactImage(request)

' Console.WriteLine($"Extracted text: {response.ExtractedText}")
 
Dim fsWr As FileStream = File.Create(redactedImagePath, FileAccess.Write)
response.RedactedImage.WriteTo(fsWr)
fsWr.Close()
fsRe.Close()
Return 0
Ultima modifica effettuata da Carlo 04/06/20 10:53
in programmazione tutto è permesso