有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

基于Play框架的java图像处理

我有以下代码可以从facebook获取个人资料图片,必要时调整其大小,并将其裁剪成一张漂亮的方形图片:

            url = new URL(profilePictureUrl);
            InputStream stream = url.openStream();

            // Create the file from the URL
            File temp = File.createTempFile(Codec.UUID(), "temp");
            OutputStream out = null;
            try {
                out = new FileOutputStream(temp);
            } catch (FileNotFoundException e1) {
                Logger.error("SF-ERROR : Error creating temp file", e1);
            }

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = stream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            stream.close();
            out.close();

            String uuid = Codec.UUID();
            File resizedFile = File.createTempFile("resized-" + uuid, ".image");
            File croppedFile = File.createTempFile("cropped-" + uuid, ".image");

            // Check that this is in fact, an image
            if (temp != null && ImageIO.read(temp) != null) {

                int height = ImageIO.read(temp).getHeight();
                int width = ImageIO.read(temp).getWidth();

                double newWidth = (height * 180) / (width);

                boolean high = false;
                boolean wide = false;

                if (newWidth > 220) {
                    high = true;
                }
                if (newWidth < 140) {
                    wide = true;
                }

                if (height > width) {
                    // Resize the image to a width of 180 pixels.
                    Images.resize(temp, resizedFile, 180, -1);
                } else {
                    // Resize the image to a height of 180 pixels.
                    Images.resize(temp, resizedFile, -1, 180);
                }

                // Crop the remaining width or height to 180 pixels. This way we get a nice square picture.
                if (high) {
                    Images.crop(resizedFile, croppedFile, 0, 20, 180, 200);
                } else if (wide) {
                    Images.crop(resizedFile, croppedFile, 20, 0, 200, 180);
                } else {
                    Images.crop(resizedFile, croppedFile, 0, 0, 180, 180);
                }
P> >宽而高的布尔值,因此作物大致在中间。p>

这是可行的,但需要花费大量时间

任何人都可以看到这是否可以更快地完成,或者我是否做错了什么


共 (0) 个答案