|
import com.aspose.imaging.cloud.sdk.model.requests.ConvertImageRequest;
|
|
import com.aspose.imaging.cloud.sdk.model.requests.CreateConvertedImageRequest;
|
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
|
|
string ImageFileName = "example_image.jpeg2000";
|
|
string ImagesFolder = "ExampleImages";
|
|
string CloudFolder = "CloudImages";
|
|
string OutputFolder = "Output";
|
|
|
|
// Get ClientId and ClientSecret from https://dashboard.aspose.cloud/
|
|
// or use on-premise version (https://docs.aspose.cloud/imaging/getting-started/how-to-run-docker-container/)
|
|
ImagingApi api = new ImagingApi(argumentValues.ClientSecret, argumentValues.ClientId, "https://api.aspose.cloud");
|
|
|
|
/**
|
|
* Convert an image to another format.
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public void convertImageFromStorage() throws Exception {
|
|
|
|
// Upload image to cloud storage
|
|
byte[] inputImage = Files.readAllBytes(Paths.get(ImagesFolder, ImageFileName));
|
|
UploadFileRequest request = new UploadFileRequest(Paths.get(CloudFolder, ImageFileName).toString(), image, null);
|
|
FilesUploadResult response = api.uploadFile(request);
|
|
if(response.getErrors() != null && response.getErrors().size() > 0)
|
|
throw new Exception("Uploading errors count: " + response.getErrors().size());
|
|
|
|
// Please refer to https://docs.aspose.cloud/imaging/supported-file-formats/#convert
|
|
// for possible output formats
|
|
String format = "dicom";
|
|
String folder = CloudFolder; // Input file is saved at the desired folder in the storage
|
|
String storage = null; // Cloud Storage name
|
|
|
|
ConvertImageRequest request = new ConvertImageRequest(ImageFileName, format, folder, storage);
|
|
byte[] updatedImage = api.convertImage(request);
|
|
|
|
// Save the image file to output folder
|
|
String convertedImageName = ImageFileName.substring(0, ImageFileName.lastIndexOf('.') + "dicom";
|
|
Path path = Paths.get(OutputFolder, convertedImageName).toAbsolutePath();
|
|
Files.write(path, updatedImage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert an image to another format. Image data is passed in a request stream.
|
|
/// </summary>
|
|
public void createConvertedImageFromRequest() throws Exception {
|
|
|
|
byte[] inputStream = Files.readAllBytes(Paths.get(ImagesFolder, ImageFileName));
|
|
|
|
// Please refer to https://docs.aspose.cloud/imaging/supported-file-formats/#convert
|
|
// for possible output formats
|
|
String format = "dicom";
|
|
String outPath = null; // Path to updated file (if this is empty, response contains streamed image)
|
|
String storage = null; // Cloud Storage name
|
|
|
|
CreateConvertedImageRequest request = new CreateConvertedImageRequest(inputStream, format, outPath, storage);
|
|
byte[] updatedImage = api.createConvertedImage(request);
|
|
|
|
// Save the image file to output folder
|
|
String convertedImageName = ImageFileName.substring(0, ImageFileName.lastIndexOf('.') + "dicom";
|
|
Path path = Paths.get(OutputFolder, convertedImageName).toAbsolutePath();
|
|
Files.write(path, updatedImage);
|
|
}
|