美文网首页
Java中使用Base64保存图片的时候同时创建不同分辨率的副本

Java中使用Base64保存图片的时候同时创建不同分辨率的副本

作者: Slience无言 | 来源:发表于2017-03-02 10:52 被阅读0次

    转自 http://elim.iteye.com/blog/1187394

        /**
         * 上传图片
         * @param base64 图片编码后的字符串
         * @return 上传成功后的文件名
         */
        public static String upload(String base64) {
            String dir = getRequest().getRealPath(UPLOAD_PATH);
            File fileLocation = new File(dir);
            String fileType = base64.substring(11, 14);
            if(!fileLocation.exists()) {
                boolean isCreated  = fileLocation.mkdir();
                if(!isCreated) {
                    //目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。
                    return null;
                }
            }
            if(base64.indexOf("jpeg") != -1) {
                //存在jpeg文件的情况
                base64 = base64.replaceFirst("jpeg", "jpg");
            }
            String tempStr = UUID.randomUUID().toString()+System.currentTimeMillis();
            
            String upName = tempStr+"."+fileType;
            FileOutputStream out;
            String iconBase64 = base64.substring(22);
            try {
                byte[] buffer = new BASE64Decoder().decodeBuffer(iconBase64);
                out = new FileOutputStream(dir+"/"+upName);
                out.write(buffer);
                out.close();
                
                String upNameXhdpi = tempStr+"-xhdpi."+fileType;
                String upNameMhdpi = tempStr+"-mhdpi."+fileType;
                
                FileOutputStream outXhdpi = new FileOutputStream(dir+"/"+upNameXhdpi);
                FileOutputStream outMhdpi = new FileOutputStream(dir+"/"+upNameMhdpi);
                
                resizeImage(iconBase64, outXhdpi, xhdpi, fileType);
                resizeImage(iconBase64, outMhdpi, mhdpi, fileType);
                
                return PATH+UPLOAD_PATH + "/" +upName;
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 改变图片的大小到宽为size,然后高随着宽等比例变化
         * @param is 上传的图片的输入流
         * @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
         * @param size 新图片的宽
         * @param format 新图片的格式
         * @throws IOException
         */
        public static void resizeImage(String is, OutputStream os, int size, String format) throws IOException {
            ByteArrayInputStream in = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(is));
            BufferedImage prevImage = ImageIO.read(in);  
            double width = prevImage.getWidth();
            double height = prevImage.getHeight();
            double percent = size/width;
            int newWidth = (int)(width * percent);
            int newHeight = (int)(height * percent);
            BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
            Graphics graphics = image.createGraphics();
            graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
            ImageIO.write(image, format, os);
            os.flush();
            in.close();
            os.close();
        }
    

    upload方法中xhdpi和mhdpi是指不同大小的数字,上传上来的base64是包含头的,例如这样的base64字符串

    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMAAAsSAAALEgHS3X78AAAI4ElEQVR42u2d2W7b2AGGP1KkRFGStdnxmgToFdHkJfICc9lO537ay2IuivYN2r7HtDOZmwJ9gE4fogPwaprFBmzHlmTtC5deKFQVL7HiiDo85PkAw7AsSD91PpGHZ9X4CK7rPgG+Al4AvwR2gAKKJNMHjoHXwL+Bl47j/PeuJ2u3Pfi+4P8C/ArIiT4ixWcRAC+BPzmO8+b6P28I4LruF8C3QEV0csVa6QO/dRznu+UH9eU/XNf9PfAPVOGnkTLwN9d1v1l+cHEGcF3318B33HFZUKSGEPiN4zgv4X1hv7/
    

    相关文章

      网友评论

          本文标题:Java中使用Base64保存图片的时候同时创建不同分辨率的副本

          本文链接:https://www.haomeiwen.com/subject/xcehgttx.html