服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现按比例缩小图片

Java实现按比例缩小图片

2022-11-20 14:28有的也 Java教程

这篇文章主要为大家详细介绍了Java实现按比例缩小图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现按比例缩小图片的具体代码,供大家参考,具体内容如下

使用spring注解上传文件@RequestParam(value="", required=true),required为true时不能传入空值,为false时反之;UploadFile.getOriginalFilename()获取上传的文件的文件名;System.nanoTime()返回当前时间的纳秒,用做文件名;FileUtils.writeByteArrayToFile()上传文件到本地目录;使用BufferedImage将图片加载到内存中,然后对图片进行修改如大小变换、图片变灰、设置透明等。 

效果图:

Java实现按比例缩小图片

HTML:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<div class="form-group">
    <label class="col-lg-1 col-md-1 col-sm-2 col-xs-2 label-size"><span class="c-red">*</span>头像:</label>
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 img-padding-zero">
        <div>
            <span id="Upload_ph" class="img-hide">
                <input type="file" title="" id="UploadFile" name="UploadFile" multiple="" class="img-input">
            </span>
            <span>
                <img alt="" src="${ctx}/UserManages/reveal_photo.do?file=tree.png">
                <a class="a-word" style="cursor:pointer;">上传图片</a>&nbsp;&nbsp;
                <a id="showApellation" class="a-word" style="font-size:12px;"></a>                
            </span>
        </div>
        <div>
            <img src="${ctx}/UserManages/reveal_photo.do?file=noImg.png" class="img-padding" width="198" height="198" alt="" id="imgOperPhoto1" onload="AutoResizeImage(198, 198, this)" title="用于头像显示">
            <img src="${ctx}/UserManages/reveal_photo.do?file=noImg_tab.png" class="img-padding" width="28" height="40" alt="" id="imgOperPhoto3" title="用于列表显示">
        </div>
    </div>
</div>

form表单提交到java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@SuppressWarnings("unused")
@RequestMapping(value="/IntOpers")
private void IntOper(@RequestParam(value="UploadFile", required=true) MultipartFile UploadFile, PrintWriter pw, HttpServletRequest request) throws IOException {
    String StrReturn = "error", FileName = "", ext = "";
    long time = 0;
    if (!UploadFile.isEmpty()) {
        if (UploadFile != null) {
            String fullName = UploadFile.getOriginalFilename();
            ext = fullName.substring(fullName.lastIndexOf("."));//获取扩展名称
            time = System.nanoTime();
            FileName = time + ext;//设置文件保存的名称
            
            //原图
            String upDir = "F:/java/upload-ssm";//文件上传路径
            FileUtils.writeByteArrayToFile(new File(upDir, FileName), UploadFile.getBytes());
            //缩小图片
            Image srcFile = ImageIO.read(new File(upDir + "/" + FileName));
            BufferedImage tag = new BufferedImage(28, 40, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(srcFile, 0, 0, 28, 40, null);
 
            String FileName_tab = time + "_tab" + ext;//设置文件保存的名称
            FileOutputStream out = new FileOutputStream(upDir + "/" + FileName_tab);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
            
            jep.setQuality(1f, true);
            encoder.encode(tag, jep);
            out.close();
            StrReturn = "success";
        }
    }
    pw.write(StrReturn);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/ljylyz/article/details/87892709

延伸 · 阅读

精彩推荐