Thumbnails java绘图工具 单元测试 产生不同的随机数

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import org.apache.commons.lang.ArrayUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.wangyun.service.LikeService;
import com.wangyun.util.RandomNumber;

public class NewsTest extends BaseJunit4Test{
	/**
     * 产生三个不同的随机数
     * 
     * @throws IOException
     */
	@Test
	public void selectNews() throws IOException{		 
		List random = RandomNumber.getRandom(3,4);	
		System.out.println(random.get(0));
		System.out.println(random.get(1));
		System.out.println(random.get(2));
	}
	
	
	@Test
	/**
     * 裁剪
     * 
     * @throws IOException
     */
    public void test6() throws IOException {
       /* *//**
         * 图片中心400*400的区域
         *//*
        Thumbnails.of("C:/image_120.jpg")
        .sourceRegion(Positions.CENTER, 400, 400)
        .size(900, 900)
        .keepAspectRatio(false)
        .toFile("C:/image_center.jpg");
        
        /**
         * 图片右下400*400的区域
         */
        /*Thumbnails.of("C:/image_120.jpg")
        .sourceRegion(Positions.BOTTOM_RIGHT, 600, 800)
        .size(900, 900)
        .keepAspectRatio(false)
        .toFile("C:/image_right.jpg");*/
        
        /**
         * 指定坐标
         */
        Thumbnails.of("C:/封面有故事02.jpg")
        .sourceRegion(30, 170, 690, 950)
        .size(600, 900)
        .keepAspectRatio(false)
        .toFile("C:/封面有故事.jpg");
    }

	
	
	@Test
	 public  void test3() throws IOException {
        /**
         * keepAspectRatio(false) 默认是按照比例缩放的
         */
		Thumbnails.of("c:3ZM643{YKE5PAZ_CS]BDO5P.png")
        .size(600, 225)
        .keepAspectRatio(false)
        .toFile("C:/center3.jpg");
    }
	
	 /**
     * 压缩图片
     */
	@Test
	public void selectNews1() throws IOException{
		int num = 1;  
		int length=16;
        double random = Math.random();  
        if (random < 0.1) {  
            random = random + 0.1;  
        }  
        for (int i = 0; i < length; i++) {  
            num = num * 10;  
        }  
        System.out.println(num);
	}
	
	/**
     * 转化图像格式
     * 
     * @throws IOException
     */
	@Test
    public void test7() throws IOException {
        /**
         * outputFormat(图像格式)
         */
		Thumbnails.of("C:/fm33.png")
        .size(600, 900).outputFormat("jpg")
        .toFile("C:/fm3.jpg");
    }
	
	 /**
     * 水印
     * 
     * @throws IOException
     */
	@Test
    public void test5() throws IOException {
        /**
         * watermark(位置,水印图,透明度)
         */
		Thumbnails.of("C:/fm11.png")
		.size(600, 900)		
        .watermark(Positions.CENTER, ImageIO.read(new File("C:/yy.jpg")), 1f)
        .outputQuality(1f).toFile("C:/imageright.jpg");
    }
}
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)//使用junit4测试
@ContextConfiguration({"classpath:applicationContext.xml"})//加载配置文件
public class BaseJunit4Test {

}
import java.util.List;
import java.util.Random;
import java.util.Vector;

/**
 * @ClassName:     随机数工具类
 * @Description:  产生不同的随机数  
 * @author:  wangyun
 * @date: 2018-5-03     上午11:57:01
 */
public class RandomNumber {
	//a为产生随机数的个数
  public static List getRandom(int a,int b){
	  //创建一个产生随机数的对象
      Random r = new Random();

      //创建一个存储随机数的集合
      Vector<Integer> v = new Vector<Integer>();

      //定义一个统计变量
      int count = 0;

      while(count < a){
          int number = r.nextInt(b);//0到list.size()大小的随机数

          //判断number是否在集合中存在
          if(!v.contains(number)){
              //不在集合中,就添加
              v.add(number);
              count++;
          }
      }

	return v;
	  
  }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值