spring boot(java)中任何图像文件的读取/获取分辨率

bsxbgnwa  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(530)

我需要读取spring引导应用程序中任何文件类型的分辨率。有什么办法吗?我搜索了很多,但没有得到任何具体的答案。你能帮我一下吗,
提前谢谢
我的javacode:-

public String getFileInfo(MultipartFile file) throws Exception {
                    BufferedOutputStream stream = null;
                    File model = new File();
                    String extension;
                    UUID uuid = null;
                    try {
                        String fileName = file.getOriginalFilename();

                        String basePath = documentBase + java.io.File.separator + accountId;

                        Long fileSize = file.getSize();
                        uuid = UUID.randomUUID();
                        this.uploadFilePath = basePath + java.io.File.separator + uuid;

                        // Make sure file has an extension and the extension is allowed
                        int i = fileName.lastIndexOf('.');

        String[] imageFileExtensions = {"jpg", "png", "gif", "JPG", "PNG", "GIF", "BMP", "bmp", "Webp", "webp"};
                    if (Arrays.asList(imageFileExtensions).contains(extension)) {
                        java.io.File uploadedFile = new java.io.File(this.uploadFilePath);
                        Metadata metadata = ImageMetadataReader.readMetadata(uploadedFile);
                        Integer imageWidth = null;
                        Integer imageHeight = null;

                        if (metadata.containsDirectoryOfType(PngDirectory.class)) {
                            PngDirectory directory = metadata.getFirstDirectoryOfType(PngDirectory.class);
                            imageWidth = directory.getInteger(PngDirectory.TAG_IMAGE_WIDTH);
                            imageHeight = directory.getInteger(PngDirectory.TAG_IMAGE_HEIGHT);
                        } else if (metadata.containsDirectoryOfType(GifHeaderDirectory.class)) {
                            GifHeaderDirectory directory = metadata.getFirstDirectoryOfType(GifHeaderDirectory.class);
                            imageWidth = directory.getInteger(GifHeaderDirectory.TAG_IMAGE_WIDTH);
                            imageHeight = directory.getInteger(GifHeaderDirectory.TAG_IMAGE_HEIGHT);
                        } else if (metadata.containsDirectoryOfType(JpegDirectory.class)) {
                            JpegDirectory directory = metadata.getFirstDirectoryOfType(JpegDirectory.class);
                            imageWidth = directory.getInteger(JpegDirectory.TAG_IMAGE_WIDTH);
                            imageHeight = directory.getInteger(JpegDirectory.TAG_IMAGE_HEIGHT);
                        } else if (metadata.containsDirectoryOfType(BmpHeaderDirectory.class)) {
                          BmpHeaderDirectory directory = metadata.getFirstDirectoryOfType(BmpHeaderDirectory.class);
                          imageWidth = directory.getInteger(BmpHeaderDirectory.TAG_IMAGE_WIDTH);
                          imageHeight = directory.getInteger(BmpHeaderDirectory.TAG_IMAGE_HEIGHT);
                        }else if (metadata.containsDirectoryOfType(WebpDirectory.class)) {
                          WebpDirectory directory = metadata.getFirstDirectoryOfType(WebpDirectory.class);
                          imageWidth = directory.getInteger(WebpDirectory.TAG_IMAGE_WIDTH);
                          imageHeight = directory.getInteger(WebpDirectory.TAG_IMAGE_HEIGHT);
                        }
                        if (imageWidth != null && imageHeight != null) {
                            model.setDimensionX(imageWidth);
                            model.setDimensionY(imageHeight);
                        }
    model.setResolution(?)
                    }
/**

**********Some code here*********

**/

model.setresolution(?)输入

46scxncf

46scxncf1#

使用bufferedimage检查这个答案
我也会用gettype检查type,以确保它成功 BufferedImage.TYPE_CUSTOM . 一般来说,任何场景的任何图像都是相当模糊的要求

相关问题