我正在尝试将一个旧的Android项目复制到一个常规的Java桌面程序中,在该程序中,该应用程序读取Dicom文件并将其像素数组数据转换为位图。由于Android的Bitmap类在常规Java中不可用,因此我尝试将像素数组数据转换为BufferedImage并将其保存为文件。
在应用中执行以下操作:
public Bitmap createBitmap(int[] pixelData) {
Bitmap bmp = Bitmap.createBitmap(pixelData, columns, rows, Bitmap.Config.ARGB_8888);
Runtime.getRuntime().gc();
return bmp;
}
这是我的代码:
public static void getImageFromArray(int[] pixels, int width, int height, String fileName) throws IOException {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0, 0, width, height, pixels);
File outputfile = new File("C:\\Users\\mathe\\eclipse-workspace\\DicomViewerFinal\\images\\" + fileName);
ImageIO.write(image, "jpg", outputfile);
}
但是我得到了这个例外:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 262144
at java.awt.image.SinglePixelPackedSampleModel.setPixels(Unknown Source)
at java.awt.image.WritableRaster.setPixels(Unknown Source)
at Main.getImageFromArray(Main.java:42)
at Main.main(Main.java:29)
我不知道应该怎么做。另外,是否有更好的方法将像素数据转换为文件?这是Dicom类和可用的信息:
public class DicomImage implements Serializable {
private File file;
private int[] pixelData;
private int[][] matrix;
//private Bitmap bitmap;
private int bitsAllocated;
private int pixelRepresentation;
private int columns;
private int rows;
private String imageType;
private int minX;
private int maxX;
private int minY;
private int maxY;
private boolean bigEndian; }