Java图片处理开源框架-Thumbnailator

<div id="article_content" class="article_content">

<div id="article_content" class="article_content">
<p style="text-indent:2em">图片处理是当今软件开发中非常重要的一环,然而处理图片的开源框架却并不多。现金网上流传的<a href="http://lib.csdn.net/base/java" class="replace_word" title="Java 知识库" target="_blank" style="color:#df3434; font-weight:bold;">Java</a>处理图片的代码,虽然可对图片进行简单处理,但效果并不理想。虽然也有些其他解决方案,但都摆脱不了繁琐,使用起来十分不方便。</p>
<p style="text-indent:2em">为了解决这个问题,我也是在网上找了好久,看了很多资料,功夫不负有心人,最终找到了一个处理图片十分棒的开源框架。特此拿出来与大家分享。</p>
<p style="text-indent:2em"><br>
</p>
<p style="text-indent:2em">Thumbnailator 是一个优秀的图片处理的Google开源Java类库。处理效果远比<a href="http://lib.csdn.net/base/java" class="replace_word" title="Java 知识库" target="_blank" style="color:#df3434; font-weight:bold;">Java </a>API的好。从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量。还支持对一个目录的所有图片进行批量处理操作。</p>
<p style="text-indent:2em">支持的处理操作:图片缩放,区域裁剪,水印,旋转,保持比例。</p>
<p style="text-indent:2em">另外值得一提的是,Thumbnailator至今仍不断更新,怎么样,感觉很有保障吧!</p>
<p style="text-indent:2em">Thumbnailator官网:<a target="_blank" href="http://code.google.com/p/thumbnailator/">http://code.google.com/p/thumbnailator/</a></p>
<p style="text-indent:2em">下面我们介绍下如何使用Thumbnailator</p>
<p style="text-indent:2em">原图:</p>
<p><img alt="" src="https://img-blog.csdn.net/20131227172325812?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3BlbmcwNDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>
<p style="text-indent:2em">1、指定大小进行缩放</p>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools"><strong>[java]</strong> <a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="ViewSource" title="view plain">
view plain</a><a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="CopyToClipboard" title="copy">copy</a><a target="_blank" href="https://code.csdn.net/snippets/130986" title="在CODE上查看代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代码片" height="12" width="12" style="position:relative; top:1px; left:2px"></a><a target="_blank" href="https://code.csdn.net/snippets/130986/fork" title="派生到我的代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到我的代码片" height="12" width="12" style="position:relative; top:2px; left:2px"></a><div style="position: absolute; left: 834px; top: 1499px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=18&height=18" wmode="transparent"></div></div>
</div>
<ol class="dp-j" start="1">
<li class="alt"><span><span class="comment">//size(宽度, 高度)</span><span>  </span></span></li><li><span>  </span></li><li class="alt"><span><span class="comment">/*</span> </span></li><li><span><span class="comment"> * 若图片横比200小,高比300小,不变</span> </span></li><li class="alt"><span><span class="comment"> * 若图片横比200小,高比300大,高缩小到300,图片比例不变</span> </span></li><li><span><span class="comment"> * 若图片横比200大,高比300小,横缩小到200,图片比例不变</span> </span></li><li class="alt"><span><span class="comment"> * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300</span> </span></li><li><span><span class="comment"> */</span><span>  </span></span></li><li class="alt"><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li><span>    .size(<span class="number">200</span><span>, </span><span class="number">300</span><span>)  </span></span></li><li class="alt"><span>    .toFile(<span class="string">"c:/a380_200x300.jpg"</span><span>);  </span></span></li><li><span>  </span></li><li class="alt"><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li><span>    .size(<span class="number">2560</span><span>, </span><span class="number">2048</span><span>)  </span></span></li><li class="alt"><span>    .toFile(<span class="string">"c:/a380_2560x2048.jpg"</span><span>);  </span></span></li></ol>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p><br>
</p>
<p style="text-indent:2em">2、按照比例进行缩放</p>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools"><strong>[java]</strong> <a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="ViewSource" title="view plain">
view plain</a><a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="CopyToClipboard" title="copy">copy</a><a target="_blank" href="https://code.csdn.net/snippets/130986" title="在CODE上查看代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代码片" height="12" width="12" style="position:relative; top:1px; left:2px"></a><a target="_blank" href="https://code.csdn.net/snippets/130986/fork" title="派生到我的代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到我的代码片" height="12" width="12" style="position:relative; top:2px; left:2px"></a><div style="position: absolute; left: 834px; top: 1921px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=18&height=18" wmode="transparent"></div></div>
</div>
<ol class="dp-j" start="1">
<li class="alt"><span><span class="comment">//scale(比例)</span><span>  </span></span></li><li><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .scale(<span class="number">0</span><span>.25f)  </span></span></li><li><span>    .toFile(<span class="string">"c:/a380_25%.jpg"</span><span>);  </span></span></li><li class="alt"><span>  </span></li><li><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .scale(<span class="number">1</span><span>.10f)  </span></span></li><li><span>    .toFile(<span class="string">"c:/a380_110%.jpg"</span><span>);  </span></span></li></ol>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p><br>
</p>
<p style="text-indent:2em">3、不按照比例,指定大小进行缩放</p>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools"><strong>[java]</strong> <a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="ViewSource" title="view plain">
view plain</a><a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="CopyToClipboard" title="copy">copy</a><a target="_blank" href="https://code.csdn.net/snippets/130986" title="在CODE上查看代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代码片" height="12" width="12" style="position:relative; top:1px; left:2px"></a><a target="_blank" href="https://code.csdn.net/snippets/130986/fork" title="派生到我的代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到我的代码片" height="12" width="12" style="position:relative; top:2px; left:2px"></a><div style="position: absolute; left: 834px; top: 2217px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=18&height=18" wmode="transparent"></div></div>
</div>
<ol class="dp-j" start="1">
<li class="alt"><span><span class="comment">//keepAspectRatio(false)默认是按照比例缩放的</span><span>  </span></span></li><li><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .size(<span class="number">200</span><span>,</span><span class="number">200</span><span>)  </span></span></li><li><span>    .keepAspectRatio(<span class="keyword">false</span><span>)  </span></span></li><li class="alt"><span>    .toFile(<span class="string">"c:/a380_200x200.jpg"</span><span>);  </span></span></li></ol>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p><br>
</p>
<p style="text-indent:2em">4、旋转</p>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools"><strong>[java]</strong> <a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="ViewSource" title="view plain">
view plain</a><a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="CopyToClipboard" title="copy">copy</a><a target="_blank" href="https://code.csdn.net/snippets/130986" title="在CODE上查看代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代码片" height="12" width="12" style="position:relative; top:1px; left:2px"></a><a target="_blank" href="https://code.csdn.net/snippets/130986/fork" title="派生到我的代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到我的代码片" height="12" width="12" style="position:relative; top:2px; left:2px"></a><div style="position: absolute; left: 834px; top: 2459px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_4" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&width=18&height=18" wmode="transparent"></div></div>
</div>
<ol class="dp-j" start="1">
<li class="alt"><span><span class="comment">//rotate(角度),正数:顺时针负数:逆时针</span><span>  </span></span></li><li><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .size(<span class="number">1280</span><span>,</span><span class="number">1024</span><span>)  </span></span></li><li><span>    .rotate(<span class="number">90</span><span>)  </span></span></li><li class="alt"><span>    .toFile(<span class="string">"c:/a380_rotate+90.jpg"</span><span>);  </span></span></li><li><span>  </span></li><li class="alt"><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li><span>    .size(<span class="number">1280</span><span>,</span><span class="number">1024</span><span>)  </span></span></li><li class="alt"><span>    .rotate(-<span class="number">90</span><span>)  </span></span></li><li><span>    .toFile(<span class="string">"c:/a380_rotate-90.jpg"</span><span>);  </span></span></li></ol>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p><img alt="" src="https://img-blog.csdn.net/20131227173231250?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3BlbmcwNDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>
<p><img alt="" src="https://img-blog.csdn.net/20131227173235421?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3BlbmcwNDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>
<p><br>
</p>
<p style="text-indent:2em">5、水印</p>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools"><strong>[java]</strong> <a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="ViewSource" title="view plain">
view plain</a><a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="CopyToClipboard" title="copy">copy</a><a target="_blank" href="https://code.csdn.net/snippets/130986" title="在CODE上查看代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代码片" height="12" width="12" style="position:relative; top:1px; left:2px"></a><a target="_blank" href="https://code.csdn.net/snippets/130986/fork" title="派生到我的代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到我的代码片" height="12" width="12" style="position:relative; top:2px; left:2px"></a><div style="position: absolute; left: 834px; top: 4099px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_5" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&width=18&height=18" wmode="transparent"></div></div>
</div>
<ol class="dp-j" start="1">
<li class="alt"><span><span class="comment">//watermark(位置,水印图,透明度)</span><span>  </span></span></li><li><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .size(<span class="number">1280</span><span>,</span><span class="number">1024</span><span>)  </span></span></li><li><span>    .watermark(Positions.BOTTOM_RIGHT,ImageIO.read(newFile(<span class="string">"images/watermark.png"</span><span>)),</span><span class="number">0</span><span>.5f)  </span></span></li><li class="alt"><span>    .outputQuality(<span class="number">0</span><span>.8f)  </span></span></li><li><span>    .toFile(<span class="string">"c:/a380_watermark_bottom_right.jpg"</span><span>);  </span></span></li><li class="alt"><span>  </span></li><li><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .size(<span class="number">1280</span><span>,</span><span class="number">1024</span><span>)  </span></span></li><li><span>    .watermark(Positions.CENTER,ImageIO.read(newFile(<span class="string">"images/watermark.png"</span><span>)),</span><span class="number">0</span><span>.5f)  </span></span></li><li class="alt"><span>    .outputQuality(<span class="number">0</span><span>.8f)  </span></span></li><li><span>    .toFile(<span class="string">"c:/a380_watermark_center.jpg"</span><span>);  </span></span></li></ol>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p><br>
<img alt="" src="https://img-blog.csdn.net/20131227173438828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3BlbmcwNDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>
<p><br>
</p>
<p><img alt="" src="https://img-blog.csdn.net/20131227173619562?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3BlbmcwNDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>
<p><br>
</p>
<p style="text-indent:2em">6、裁剪</p>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools"><strong>[java]</strong> <a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="ViewSource" title="view plain">
view plain</a><a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="CopyToClipboard" title="copy">copy</a><a target="_blank" href="https://code.csdn.net/snippets/130986" title="在CODE上查看代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代码片" height="12" width="12" style="position:relative; top:1px; left:2px"></a><a target="_blank" href="https://code.csdn.net/snippets/130986/fork" title="派生到我的代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到我的代码片" height="12" width="12" style="position:relative; top:2px; left:2px"></a><div style="position: absolute; left: 834px; top: 5585px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_6" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&width=18&height=18" wmode="transparent"></div></div>
</div>
<ol class="dp-j" start="1">
<li class="alt"><span><span class="comment">//sourceRegion()</span><span>  </span></span></li><li><span>  </span></li><li class="alt"><span><span class="comment">//图片中心400*400的区域</span><span>  </span></span></li><li><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .sourceRegion(Positions.CENTER,<span class="number">400</span><span>,</span><span class="number">400</span><span>)  </span></span></li><li><span>    .size(<span class="number">200</span><span>,</span><span class="number">200</span><span>)  </span></span></li><li class="alt"><span>    .keepAspectRatio(<span class="keyword">false</span><span>)  </span></span></li><li><span>    .toFile(<span class="string">"c:/a380_region_center.jpg"</span><span>);  </span></span></li><li class="alt"><span>  </span></li><li><span><span class="comment">//图片右下400*400的区域</span><span>  </span></span></li><li class="alt"><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li><span>    .sourceRegion(Positions.BOTTOM_RIGHT,<span class="number">400</span><span>,</span><span class="number">400</span><span>)  </span></span></li><li class="alt"><span>    .size(<span class="number">200</span><span>,</span><span class="number">200</span><span>)  </span></span></li><li><span>    .keepAspectRatio(<span class="keyword">false</span><span>)  </span></span></li><li class="alt"><span>    .toFile(<span class="string">"c:/a380_region_bootom_right.jpg"</span><span>);  </span></span></li><li><span>  </span></li><li class="alt"><span><span class="comment">//指定坐标</span><span>  </span></span></li><li><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .sourceRegion(<span class="number">600</span><span>,</span><span class="number">500</span><span>,</span><span class="number">400</span><span>,</span><span class="number">400</span><span>)  </span></span></li><li><span>    .size(<span class="number">200</span><span>,</span><span class="number">200</span><span>)  </span></span></li><li class="alt"><span>    .keepAspectRatio(<span class="keyword">false</span><span>)  </span></span></li><li><span>    .toFile(<span class="string">"c:/a380_region_coord.jpg"</span><span>);  </span></span></li></ol>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p><br>
</p>
<p><img alt="" src="https://img-blog.csdn.net/20131227173924828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3BlbmcwNDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>
<p><img alt="" src="https://img-blog.csdn.net/20131227173901671?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3BlbmcwNDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>
<p><img alt="" src="https://img-blog.csdn.net/20131227173941015?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3BlbmcwNDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>
<p><br>
</p>
<p style="text-indent:2em">7、转化图像格式</p>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools"><strong>[java]</strong> <a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="ViewSource" title="view plain">
view plain</a><a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="CopyToClipboard" title="copy">copy</a><a target="_blank" href="https://code.csdn.net/snippets/130986" title="在CODE上查看代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代码片" height="12" width="12" style="position:relative; top:1px; left:2px"></a><a target="_blank" href="https://code.csdn.net/snippets/130986/fork" title="派生到我的代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到我的代码片" height="12" width="12" style="position:relative; top:2px; left:2px"></a><div style="position: absolute; left: 834px; top: 6515px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_7" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_7" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&width=18&height=18" wmode="transparent"></div></div>
</div>
<ol class="dp-j" start="1">
<li class="alt"><span><span class="comment">//outputFormat(图像格式)</span><span>  </span></span></li><li><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .size(<span class="number">1280</span><span>,</span><span class="number">1024</span><span>)  </span></span></li><li><span>    .outputFormat(<span class="string">"png"</span><span>)  </span></span></li><li class="alt"><span>    .toFile(<span class="string">"c:/a380_1280x1024.png"</span><span>);  </span></span></li><li><span>  </span></li><li class="alt"><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li><span>    .size(<span class="number">1280</span><span>,</span><span class="number">1024</span><span>)  </span></span></li><li class="alt"><span>    .outputFormat(<span class="string">"gif"</span><span>)  </span></span></li><li><span>    .toFile(<span class="string">"c:/a380_1280x1024.gif"</span><span>);  </span></span></li></ol>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p style="text-indent:2em"> </p>
<p style="text-indent:2em">8、输出到OutputStream</p>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools"><strong>[java]</strong> <a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="ViewSource" title="view plain">
view plain</a><a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="CopyToClipboard" title="copy">copy</a><a target="_blank" href="https://code.csdn.net/snippets/130986" title="在CODE上查看代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代码片" height="12" width="12" style="position:relative; top:1px; left:2px"></a><a target="_blank" href="https://code.csdn.net/snippets/130986/fork" title="派生到我的代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到我的代码片" height="12" width="12" style="position:relative; top:2px; left:2px"></a><div style="position: absolute; left: 834px; top: 6847px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_8" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&width=18&height=18" wmode="transparent"></div></div>
</div>
<ol class="dp-j" start="1">
<li class="alt"><span><span class="comment">//toOutputStream(流对象)</span><span>  </span></span></li><li><span>OutputStreamos=newFileOutputStream(<span class="string">"c:/a380_1280x1024_OutputStream.png"</span><span>);  </span></span></li><li class="alt"><span>Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li><span>    .size(<span class="number">1280</span><span>,</span><span class="number">1024</span><span>)  </span></span></li><li class="alt"><span>    .toOutputStream(os);  </span></li></ol>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p style="text-indent:2em"> </p>
<p style="text-indent:2em"> 9、输出到BufferedImage</p>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools"><strong>[java]</strong> <a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="ViewSource" title="view plain">
view plain</a><a target="_blank" href="http://blog.csdn.net/wangpeng047/article/details/17610451#" class="CopyToClipboard" title="copy">copy</a><a target="_blank" href="https://code.csdn.net/snippets/130986" title="在CODE上查看代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/CODE_ico.png" alt="在CODE上查看代码片" height="12" width="12" style="position:relative; top:1px; left:2px"></a><a target="_blank" href="https://code.csdn.net/snippets/130986/fork" title="派生到我的代码片" style="text-indent:0"><img src="https://code.csdn.net/assets/ico_fork.svg" alt="派生到我的代码片" height="12" width="12" style="position:relative; top:2px; left:2px"></a><div style="position: absolute; left: 834px; top: 7089px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_9" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="18" height="18" name="ZeroClipboardMovie_9" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=9&width=18&height=18" wmode="transparent"></div></div>
</div>
<ol class="dp-j" start="1">
<li class="alt"><span><span class="comment">//asBufferedImage()返回BufferedImage</span><span>  </span></span></li><li><span>BufferedImagethumbnail=Thumbnails.of(<span class="string">"images/a380_1280x1024.jpg"</span><span>)  </span></span></li><li class="alt"><span>    .size(<span class="number">1280</span><span>,</span><span class="number">1024</span><span>)  </span></span></li><li><span>    .asBufferedImage();  </span></li><li class="alt"><span>ImageIO.write(thumbnail,<span class="string">"jpg"</span><span>,newFile(</span><span class="string">"c:/a380_1280x1024_BufferedImage.jpg"</span><span>));  </span></span></li></ol>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p style="text-indent:2em"> </p>
</div>
   
</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值