[英]Problem with Width and Height properity of a image on java
我在 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 兆字节)
问题是我使用的是 Java 8,所以要加载 TIFF 图像,我们需要一个插件。
将这些行添加到您的 pom.xml 中:
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.4.0</version>
</dependency>
如果您需要使用 jar 或其他存储库,请在github 中查看更多信息
并在代码上做这些更改:
FileInputStream fis = new FileInputStream(file);
BufferedImage bufferedImage = ImageIO.read(fis);
System.out.println("Width: " + bufferedImage.getWidth());
Image img = SwingFXUtils.toFXImage(bufferedImage, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.