Search in sources :

Example 1 with RedactImageRequest

use of com.google.privacy.dlp.v2.RedactImageRequest in project java-docs-samples by GoogleCloudPlatform.

the class Redact method redactImage.

// [START dlp_redact_image]
/*
   * Redact sensitive data from an image using the Data Loss Prevention API.
   *
   * @param filePath The path to a local file to inspect. Can be a JPG or PNG image file.
   * @param minLikelihood The minimum likelihood required before redacting a match.
   * @param infoTypes The infoTypes of information to redact.
   * @param outputPath The local path to save the resulting image to.
   * @param projectId The project ID to run the API call under.
   */
private static void redactImage(String filePath, Likelihood minLikelihood, List<InfoType> infoTypes, String outputPath, String projectId) throws Exception {
    // Instantiate the DLP client
    try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
        String mimeType = URLConnection.guessContentTypeFromName(filePath);
        if (mimeType == null) {
            mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
        }
        ByteContentItem.BytesType bytesType;
        switch(mimeType) {
            case "image/jpeg":
                bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
                break;
            case "image/bmp":
                bytesType = ByteContentItem.BytesType.IMAGE_BMP;
                break;
            case "image/png":
                bytesType = ByteContentItem.BytesType.IMAGE_PNG;
                break;
            case "image/svg":
                bytesType = ByteContentItem.BytesType.IMAGE_SVG;
                break;
            default:
                bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
                break;
        }
        byte[] data = Files.readAllBytes(Paths.get(filePath));
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).build();
        ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(bytesType).setData(ByteString.copyFrom(data)).build();
        List<RedactImageRequest.ImageRedactionConfig> imageRedactionConfigs = infoTypes.stream().map(infoType -> RedactImageRequest.ImageRedactionConfig.newBuilder().setInfoType(infoType).build()).collect(Collectors.toList());
        RedactImageRequest redactImageRequest = RedactImageRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).addAllImageRedactionConfigs(imageRedactionConfigs).setByteItem(byteContentItem).setInspectConfig(inspectConfig).build();
        RedactImageResponse redactImageResponse = dlpClient.redactImage(redactImageRequest);
        // redacted image data
        ByteString redactedImageData = redactImageResponse.getRedactedImage();
        FileOutputStream outputStream = new FileOutputStream(outputPath);
        outputStream.write(redactedImageData.toByteArray());
        outputStream.close();
    }
}
Also used : ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) Options(org.apache.commons.cli.Options) Likelihood(com.google.privacy.dlp.v2.Likelihood) HelpFormatter(org.apache.commons.cli.HelpFormatter) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) RedactImageResponse(com.google.privacy.dlp.v2.RedactImageResponse) ArrayList(java.util.ArrayList) DefaultParser(org.apache.commons.cli.DefaultParser) ServiceOptions(com.google.cloud.ServiceOptions) URLConnection(java.net.URLConnection) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) Files(java.nio.file.Files) CommandLineParser(org.apache.commons.cli.CommandLineParser) FileOutputStream(java.io.FileOutputStream) InfoType(com.google.privacy.dlp.v2.InfoType) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) List(java.util.List) ProjectName(com.google.privacy.dlp.v2.ProjectName) RedactImageRequest(com.google.privacy.dlp.v2.RedactImageRequest) Paths(java.nio.file.Paths) ParseException(org.apache.commons.cli.ParseException) ByteString(com.google.protobuf.ByteString) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ByteString(com.google.protobuf.ByteString) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) RedactImageRequest(com.google.privacy.dlp.v2.RedactImageRequest) FileOutputStream(java.io.FileOutputStream) RedactImageResponse(com.google.privacy.dlp.v2.RedactImageResponse)

Aggregations

ServiceOptions (com.google.cloud.ServiceOptions)1 DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)1 ByteContentItem (com.google.privacy.dlp.v2.ByteContentItem)1 InfoType (com.google.privacy.dlp.v2.InfoType)1 InspectConfig (com.google.privacy.dlp.v2.InspectConfig)1 Likelihood (com.google.privacy.dlp.v2.Likelihood)1 ProjectName (com.google.privacy.dlp.v2.ProjectName)1 RedactImageRequest (com.google.privacy.dlp.v2.RedactImageRequest)1 RedactImageResponse (com.google.privacy.dlp.v2.RedactImageResponse)1 ByteString (com.google.protobuf.ByteString)1 FileOutputStream (java.io.FileOutputStream)1 URLConnection (java.net.URLConnection)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1