使用 Aspose.Total for Java ,您可以在任何 Java 应用程序中通过两个简单的步骤将 JSON 格式转换为 DICOM。首先,通过使用 Aspose.Cells for Java ,您可以将 JSON 解析为 JPEG。之后,通过使用 Aspose.Imaging for Java ,您可以将 JPEG 转换为 DICOM。
通过 Java 将 JSON 格式转换为 DICOM
- 建 Workbook 对象并打开JSON文件
- 使用 [save]( https://reference.aspose.com/cells/java/com.aspose.cells/workbook#save(java.lang.String,%20com.aspose.cells.SaveOptions) 将 JSON 保存为 JPEG ) 方法
- 使用 Image 类加载 JPEG 文档
- 使用 save 将文档保存为 DICOM 格式-) 方法
// open JSON file using Workbook object | |
Workbook workbook = new Workbook("input.json"); | |
// save resultant file in JSON-TO-JPEG ormat | |
workbook.save("output.jpeg", SaveFormat.AUTO); | |
// load JPEG file | |
Image image = Image.Load("output.jpeg"); | |
// supports DICOM, JPEG2000, APNG, PSD, DXF, WMF, EMZ, WMZ, TGA, and SVGZ file formats | |
// save JPEG to PSD file format | |
image.save("output.psd", new PsdOptions()); |
通过 Java 设置布局并将 JSON 格式转换为 DICOM
此外,该 API 允许您使用指定的布局选项将 JSON 解析为 DICOM。为了指定布局选项,您可以使用 JsonLayoutOptions 类。它允许您将数组作为表格处理、忽略空值、忽略数组标题、忽略对象标题、将字符串转换为数字或日期、设置日期和数字格式以及设置标题样式。所有这些选项都允许您根据需要呈现数据。以下代码片段向您展示了如何设置布局选项。
// create a blank Workbook object | |
Workbook workbook = new Workbook("input.json"); | |
// access default empty worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// read JSON file | |
String jsonInput = new String(Files.readAllBytes("SampleJson.json")); | |
// set JsonLayoutOptions for formatting | |
JsonLayoutOptions layoutOptions = new JsonLayoutOptions(); | |
layoutOptions.setArrayAsTable(true); | |
layoutOptionssetConvertNumericOrDate(true); | |
layoutOptionssetIgnoreArrayTitle(true); | |
layoutOptionssetIgnoreNull(true); | |
layoutOptionssetIgnoreObjectTitle(true); | |
// import JSON data to default worksheet starting at cell A1 | |
JsonUtility.importData(jsonInput, worksheet.getCells(), 0, 0, layoutOptions); | |
// save resultant file in JSON-TO-JPEG ormat | |
workbook.save("output.jpeg", SaveFormat.AUTO); | |
// load JPEG file | |
Image image = Image.Load("output.jpeg"); | |
// supports DICOM, JPEG2000, APNG, PSD, DXF, WMF, EMZ, WMZ, TGA, and SVGZ file formats | |
// save JPEG to PSD file format | |
image.save("output.psd", new PsdOptions()); |
通过 Java 将 JSON 格式转换为带水印的 DICOM
使用 API,您还可以在 DICOM 文档中将 JSON 转换为带有水印的 DICOM。为了添加水印,您可以先将 JSON 转换为 JPEG 并在其中添加水印。为了添加水印,使用 Image 类加载图像文件,创建 Graphics 的对象://apireference.aspose.com/imaging/java/com.aspose.imaging/Graphics)类并用Image对象初始化,创建一个新的[Matrix]( https://reference.aspose.com/imaging/java/ com.aspose.imaging/Matrix) 对象并将平移和变换设置为所需的角度,并使用 [Graphics.drawString]( https://reference.aspose.com/imaging/java/com.aspose.imaging/Graphics# 添加水印drawString-java.lang.String-com.aspose.imaging.Font-com.aspose.imaging.Brush-float-float-) 方法。在图像中添加水印后,您可以将 JPEG 保存为 DICOM 格式。
// open JSON file using Workbook object | |
Workbook workbook = new Workbook("input.json"); | |
// save resultant file in JSON-TO-JPEG ormat | |
workbook.save("output.jpeg", SaveFormat.AUTO); | |
// load JPEG | |
Image image = Image.load("output.jpeg"); | |
// create and initialize an instance of Graphics class | |
Graphics graphics= new Graphics(image); | |
// create an instance of Font | |
Font font = new Font("Times New Roman", 16, FontStyle.Bold); | |
// create an instance of SolidBrush and set its properties | |
SolidBrush brush = new SolidBrush(); | |
brush.setColor(Color.getBlack()); | |
brush.setOpacity(100); | |
Size sz = graphics.getImage().getSize(); | |
// create an object of Matrix class for transformation | |
Matrix matrix = new Matrix(); | |
// first a translation then a rotation | |
matrix.translate(sz.getWidth() / 2, sz.getHeight() / 2); | |
matrix.rotate(-45.0f); | |
// set the Transformation through Matrix | |
graphics.setTransform(matrix); | |
// draw a string using the SolidBrush and Font objects at specific point | |
graphics.drawString("Watermark by Aspose.Imaging for Java", font, brush, 0, 0); | |
// supports Dicom, Jpeg2000, Apng, Psd, Dxf, Wmf, Emz, Wmz, Tga, Svgz file formats | |
// save JPEG to PSD file format | |
image.save("output.psd", new PsdOptions()); |