zoukankan      html  css  js  c++  java
  • java图片处理


    import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.geometry.Positions; public class Test { public static void main(String[] args) throws IOException { Test thumbnailatorTest = new Test(); // thumbnailatorTest.test1(); // thumbnailatorTest.test2(); // thumbnailatorTest.test3(); // thumbnailatorTest.test4(); // thumbnailatorTest.test5(); // thumbnailatorTest.test6(); thumbnailatorTest.test7(); // thumbnailatorTest.test8(); // thumbnailatorTest.test9(); } /** * 指定大小进行缩放 * * @throws IOException */ private void test1() throws IOException { /* * size(width,height) 若图片横比200小,高比300小,不变 * 若图片横比200小,高比300大,高缩小到300,图片比例不变 若图片横比200大,高比300小,横缩小到200,图片比例不变 * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300 */ Thumbnails.of("E:\图片\15.jpg").size(200, 300).toFile( "E:\图片\image_200x300.jpg"); Thumbnails.of("E:\图片\15.jpg").size(2560, 2048).toFile( "E:\图片\image_2560x2048.jpg"); } /** * 按照比例进行缩放 * * @throws IOException */ private void test2() throws IOException { /** * scale(比例) */ Thumbnails.of("E:\图片\15.jpg").scale(0.25f) .toFile("E:\图片\image_25%.jpg"); Thumbnails.of("E:\图片\15.jpg").scale(1.10f).toFile( "E:\图片\image_110%.jpg"); } /** * 不按照比例,指定大小进行缩放 * * @throws IOException */ private void test3() throws IOException { /** * keepAspectRatio(false) 默认是按照比例缩放的 */ Thumbnails.of("E:\图片\15.jpg").size(120, 120).keepAspectRatio(false) .toFile("E:\图片\image_120x120.jpg"); } /** * 旋转 * * @throws IOException */ private void test4() throws IOException { /** * rotate(角度),正数:顺时针 负数:逆时针 */ Thumbnails.of("E:\图片\15.jpg").size(1280, 1024).rotate(90).toFile( "E:\图片\image+90.jpg"); Thumbnails.of("E:\图片\15.jpg").size(1280, 1024).rotate(-90).toFile( "E:\图片\iamge-90.jpg"); } /** * 水印 * * @throws IOException */ private void test5() throws IOException { /** * watermark(位置,水印图,透明度) */ Thumbnails.of("E:\图片\images/15.jpg").size(1280, 1024).watermark( Positions.BOTTOM_RIGHT, ImageIO.read(new File("E:\图片\watermark.png")), 0.5f) .outputQuality(0.8f).toFile( "E:\图片\image_watermark_bottom_right.jpg"); Thumbnails.of("E:\图片\15.jpg").size(1280, 1024).watermark( Positions.CENTER, ImageIO.read(new File("E:\图片\watermark.png")), 0.5f) .outputQuality(0.8f).toFile("E:\图片\image_watermark_center.jpg"); } /** * 裁剪 * * @throws IOException */ private void test6() throws IOException { /** * 图片中心400*400的区域 */ Thumbnails.of("E:\图片\15.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false).toFile( "E:\图片\image_region_center.jpg"); /** * 图片右下400*400的区域 */ Thumbnails.of("E:\图片\15.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false).toFile( "E:\图片\image_region_bootom_right.jpg"); /** * 指定坐标 */ Thumbnails.of("E:\图片\15.jpg").sourceRegion(600, 500, 400, 400).size( 200, 200).keepAspectRatio(false).toFile( "E:\图片\image_region_coord.jpg"); } /** * 转化图像格式 * * @throws IOException */ private void test7() throws IOException { /** * outputFormat(图像格式) */ Thumbnails.of("E:\图片\15.jpg").size(1280, 1024).outputFormat("png") .toFile("E:\图片\image_1280x1024.png"); Thumbnails.of("E:\图片\15.jpg").size(1280, 1024).outputFormat("gif") .toFile("E:\图片\image_1280x1024.gif"); } /** * 输出到OutputStream * * @throws IOException */ private void test8() throws IOException { /** * toOutputStream(流对象) */ OutputStream os = new FileOutputStream( "E:\图片\image_1280x1024_OutputStream.png"); Thumbnails.of("E:\图片\15.jpg").size(1280, 1024).toOutputStream(os); } /** * 输出到BufferedImage * * @throws IOException */ private void test9() throws IOException { /** * asBufferedImage() 返回BufferedImage */ BufferedImage thumbnail = Thumbnails.of("E:\图片\15.jpg").size(1280, 1024).asBufferedImage(); ImageIO.write(thumbnail, "jpg", new File( "E:\图片\image_1280x1024_BufferedImage.jpg")); } }

     需要jar包:thumbnailator-0.4.2-all.jar

  • 相关阅读:
    MsSql “with”用法
    linq更新数据冲突问题解决
    sql server 查询所有的数据库名称
    不能成功加入域的问题
    系统安全之关闭那些存在隐患的高危端口
    winform窗体上的所有控件都不显示
    sql server 创建主键
    关于sql server trace的警告信息
    ASP.NET配置文件Web.config
    运行报表提示错误:Retrieving the COM class factory for component with CLSID {11BD526015B6412D80DB12BB60B8FE50}
  • 原文地址:https://www.cnblogs.com/a757956132/p/4300732.html
Copyright © 2011-2022 走看看