package challenge;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.text.Format;
import java.text.SimpleDateFormat;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.ObjectMetadata;

public class WaterMark {	  
	public static String fileName="世界地图.jpg"; //本地待上传图片文件名
	public static String uploadFilePath="C:/LAB_OSS/"+fileName; //本地待上传图片路径
	public static Format format = new SimpleDateFormat("yyyyMMdd");
	public static Random r = new Random();
	//上传到OSS中的对象名,为“原始图片名+日期+随机数”.jpg
	public static String objectName = "世界地图" + format.format(new Date())+r.nextInt(1000)+".jpg";
	public static String new_pic_name = "c:/LAB_OSS/旅游.jpg"; //处理后下载到本地的图片文件名
	public static String uri="";

	
	//请输入你最想去地方的名称代替xxxx,例如:法国卢浮宫,非洲大草原等等
	public static String place="法国卢浮宫";

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
        
		System.out.println("Started...");
        // 使用默认的OSS服务器创建OSSClient对象
		OSSClient client = new OSSClient(Conf.ACCESS_ID, Conf.ACCESS_KEY);
        
        //上传图片
		uploadFile(client, Conf.BUCKET_NAME, objectName, uploadFilePath);

		uri = Conf.URI + "/" + objectName;
		System.out.println("原始图片URI为: "+ uri+",请在本地浏览器中访问。 ");
      
        //设置缩放参数 set thumbnail parameters
		String width="500";
		String height="500";
		String thumbnail=width+"w_"+height+"h.jpg";
		String uri_thumbnail = uri+"@"+thumbnail;
		System.out.println("缩略后的网站图片的URI为: "+uri_thumbnail+",请在本地浏览器中访问。 ");
        
        //设置水印参数 set watermark parameters
		String watermark = "watermark=2"; // watermark type, 1=pic mark, 2=text mark;
		String font_type = "&type=" + BinaryUtil.toBase64String("fangzhengheiti".getBytes());
		String size = "&size=" + "30";
		String text = "世界那么大, 我想去看看:"+place;
		text = "&text=" + BinaryUtil.toBase64String(text.getBytes());
		String color = "&color=" + BinaryUtil.toBase64String("#FF0033".getBytes());
		String position = "&p=" + "4";
		String transparency = "&t=" + "70";
		watermark = watermark + font_type + size + text + color + position + transparency;
		String uri_watermark = uri +"@"+ watermark;
		System.out.println("打水印后图片的URI为: "+ uri_watermark+",请在本地浏览器中访问。 ");
        
        //设置管道参数 set pipe parameters
		String pipe=thumbnail+"|"+watermark;
		String uri_pipe=uri+"@"+pipe;
		System.out.println("通过管道访问缩略和打水印双重处理后图片的URI为: "+ uri_pipe+",请在本地浏览器中访问。 ");
		
		//下载图片
		String uri_pipe_download=uri+"@"+thumbnail+"%7C"+ watermark;//“%7C”是管道符“|”的转义字符
		downloadFile(uri_pipe_download);
		System.out.println("Finished...");
        
	}
	
	// 上传文件
	private static void uploadFile(OSSClient client, String bucketName, String key,String filePath)
	       throws OSSException, ClientException, FileNotFoundException {
	    File file = new File(filePath);
	    ObjectMetadata objectMeta = new ObjectMetadata();
	objectMeta.setContentLength(file.length());
	    // 可以在metadata中标记文件类型
	    objectMeta.setContentType("image/jpeg");
	    System.out.println("开始上传文件"+filePath);
	    InputStream input = new FileInputStream(file);
	        client.putObject(bucketName, key, input, objectMeta);
	        System.out.println("上传成功!");
	}
    
    // 下载文件
	  private static void downloadFile(String uri)
	            throws OSSException, ClientException, ClientProtocolException, IOException {
	        //send requests
	        DefaultHttpClient httpClient = new DefaultHttpClient();
	        HttpResponse response = httpClient.execute(new HttpGet(uri));
	        System.out.println("开始从OSS下载文件...");
	        //write to file
	        byte[] result = EntityUtils.toByteArray(response.getEntity());
	        FileOutputStream fos = new FileOutputStream(new_pic_name);
	        fos.write(result);
	        fos.flush();
	        System.out.println("下载成功");
	}
}