gpt4 book ai didi

java - java中如何获取图片大小

转载 作者:行者123 更新时间:2023-12-01 11:00:23 25 4
gpt4 key购买 nike

应用程序想要检查现有上传的图像,是否超出最大限制。

这是 Controller

@RequestMapping(value = "/product/{id}/{id2}", method = RequestMethod.GET,
produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public RestMethod validateFileSize(HttpSession session,
@PathVariable("id") String fileName, @PathVariable("id2") String fileType) throws Exception {
if (!fileName.isEmpty()) {
URL url = new URL("http://localhost:8080/MyShop/image/" + fileName + "." + fileType +"/");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = url.openStream();
byte[] b = new byte[2^16];
int read = is.read(b);

while (read > -1) {
baos.write(b, 0, read);
read = is.read(b);
}

int size = baos.toByteArray().length;

if (size <= 102400) {
return new RestMethod(null, true);
} else {
return new RestMethod(null, false);
}
} else {
throw new ApplicationException(ErrorCategory.UNSPECIFIED, "noFile");
}
} catch (Exception e) {
if (e instanceof ApplicationException) {
ApplicationException applicationException = (ApplicationException) e;
return new RestMethod(applicationException.getErrorMessage(), false);
} else {
return new RestMethod(e.getMessage(), false);
}
}
}

实际大小是 760KB,即 77820 Byte,但它返回 3475。为什么 size 没有返回正确的图像大小?如何获取图像实际大小?

最佳答案

很可能是文件问题,或者您读取了错误的文件,或者读取文件的代码(或进程)存在问题。

您是否尝试过使用另一个图像文件?尝试使用操作系统通常提供的示例图像文件。这些通常会有正确的编码和元数据。

获取图像大小的代码部分是正确的。我复制了该部分并将其放入桌面 java 程序中,我得到了正确的文件大小。

您的代码在下面的桌面java程序中对我有用。

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class SimpleConvertImage2 {

public static void main(String[] args) throws FileNotFoundException, IOException {
/*
* 1. How to convert an image file to byte array?
*/

File file = new File("C:\\rose.jpg");

FileInputStream fis = new FileInputStream(file);
//create FileInputStream which obtains input bytes from a file in a file system
//FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

ByteArrayOutputStream bos = new ByteArrayOutputStream();


ByteArrayOutputStream baos = new ByteArrayOutputStream();
//InputStream is = url.openStream();
byte[] b = new byte[2^16];
int read = fis.read(b);

while (read > -1) {
baos.write(b, 0, read);
read = fis.read(b);
}

int size = baos.toByteArray().length;

System.out.println("image size original code : " + size);


}
}

这是一个示例程序,我在处理读取和写入图像文件的图像时用作引用。这也可能对您有帮助。

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class SimpleConvertImage {

public static void main(String[] args) throws FileNotFoundException, IOException {
/*
* 1. How to convert an image file to byte array?
*/

File file = new File("C:\\rose.jpg");

FileInputStream fis = new FileInputStream(file);
//create FileInputStream which obtains input bytes from a file in a file system
//FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
//Writes to this byte array output stream
bos.write(buf, 0, readNum);
System.out.println("read " + readNum + " bytes,");
}
} catch (IOException ex) {
Logger.getLogger(SimpleConvertImage.class.getName()).log(Level.SEVERE, null, ex);
}

byte[] bytes = bos.toByteArray();

int size = bytes.length;

System.out.println("image size : " + size);

/*
* 2. How to convert byte array back to an image file?
*/

ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");

//ImageIO is a class containing static methods for locating ImageReaders
//and ImageWriters, and performing simple encoding and decoding.

ImageReader reader = (ImageReader) readers.next();
Object source = bis;
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();

Image image = reader.read(0, param);
//got an image file

BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
//bufferedImage is the RenderedImage to be written

Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, null, null);

File imageFile = new File("C:\\newrose2.jpg");
ImageIO.write(bufferedImage, "jpg", imageFile);

System.out.println(imageFile.getPath());
}
}

关于java - java中如何获取图片大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33382749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com