我在 java 上的图像的宽度和高度属性有问题。出于某种原因,它们为空。
在图像的属性上,我有文件的尺寸。
当图像显示在屏幕上时,我需要控制尺寸以免出现异常。因为我必须在放大或缩小时计算尺寸以及其他一些东西。
这是加载和验证图像宽度和高度的代码
if (confirmOverwrite()) {
FileChooser fileChooser = new FileChooser();
if (previousImportDir != null) {
fileChooser.setInitialDirectory(previousImportDir.getParentFile());
}
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Arquivos de Imagem", "*.png", "*.jpg", "*.bmp", "*.gif"));
fileChooser.setTitle("Importar arquivo de imagem");
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
previousImportDir = file;
Image img = new Image("file:" + file.getPath());
if ((img.getWidth() != 0 && img.getHeight() != 0))
actionSetNewImageOnCanvas(img);
并且图像是在另一个软件上生成的,这是一个用于获取口内 X 射线的 sdk。
保存图像的sdk代码是:
private void SaveImage(short[] data, int widht, int height)
{
try
{
Bitmap pic = new Bitmap(widht, height, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
Rectangle dimension = new Rectangle(0, 0, pic.Width, pic.Height);
BitmapData picData = pic.LockBits(dimension, ImageLockMode.ReadWrite, pic.PixelFormat);
IntPtr pixelStartAddress = picData.Scan0;
//Marshal.Copy(data, 0, pixelStartAddress, data.Length);
Marshal.Copy(data, 0, pixelStartAddress, data.Length);
pic.UnlockBits(picData);
//SaveBmp(pic, Path.Combine(Directory.GetCurrentDirectory(), "imagem"));
SaveBmp(pic, "C:\\Users\\WIM\\Desktop\\teste\\teste\\teste.bmp");
pic.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private static void SaveBmp(Bitmap bmp, string path)
{
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bitmapData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
var pixelFormats = ConvertBmpPixelFormat(bmp.PixelFormat);
BitmapSource source = BitmapSource.Create(bmp.Width,
bmp.Height,
bmp.HorizontalResolution,
bmp.VerticalResolution,
pixelFormats,
null,
bitmapData.Scan0,
bitmapData.Stride * bmp.Height,
bitmapData.Stride);
bmp.UnlockBits(bitmapData);
FileStream stream = new FileStream(path, FileMode.Create);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Compression = TiffCompressOption.Zip;
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(stream);
stream.Close();
}
private static System.Windows.Media.PixelFormat ConvertBmpPixelFormat(System.Drawing.Imaging.PixelFormat pixelformat)
{
System.Windows.Media.PixelFormat pixelFormats = System.Windows.Media.PixelFormats.Default;
switch (pixelformat)
{
case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
pixelFormats = PixelFormats.Bgr32;
break;
case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
pixelFormats = PixelFormats.Gray8;
break;
case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
pixelFormats = PixelFormats.Gray16;
break;
}
return pixelFormats;
}
我如何建议我尝试使用feredImage bufferedImage = ImageIO.read(file);
而不是Image img = new Image("file:" + file.getPath());
但对象为空
根据要求,这就是我要加载的图像:https ://shortest.link/KFQ (超过 2 兆字节)