SWT中的bitmapdata,和自己封装的image工具类

关于swt中image的图片信息保存,bitmapdata的用法

bitmapdata其实就是一个图片的信息值,比如一个图片是100*100的size,那么它就有10000个像素点,每个像素点其实就一个颜色值。这个颜色值就是一个32位的ARGB值,用uint整数类型表示,即alpha 和red green blue 三原色。这样的数组组成了所有的图片。0xFF669900表示一个不透明的橙色,其中0X 表示16进制,FF就是就是16进制中2位数的最大值,0XFF相当于100%的意思,669900就是三原色。然后这10000个像素点再交给cpu描绘,就成了图片了。

所以,如果能将bitmapdata保存起来,其实可以达到减少内存的浪费。

 

以下是我封装好的image类,包含了对bitmapdata的存储,和再利用

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Display;

/**
 * @author cr.wu
 * */
public class ImageFoctory {
	
	private static HashMap<String, ImageData> hm = new HashMap<String, ImageData>();
	/**
	 * 读取图片,并设置size
	 * */
	public static Image getImg(Display d,String path,int width,int height){
		ImageData data = hm.get(path);
		if(data == null){
			File file = new File(path);
			Image image ;
			if(path.contains("http")){
				image = getNetJPG(path);
			}else{
				if(!file.exists()){
					return null;
				}
				image = new Image(d, path);
			}
			
			data = image.getImageData();
			hm.put(path, data);
		}
		Image image  = new Image(d, data);
		return setSize(image,width,height);
	}
	public static Image getImg(String path,int width,int height){
		return getImg(Display.getCurrent(), path,width,height);
	}
	
	public static Image getImg(String path){
		return getImg( path,-1,-1);
	}
	/**
	 * 设置图片的大小?
	 * */
	public static Image setSize(Image image ,int width,int height){
		if(width == -1&&height == -1){
			return image;
		}
		if(height == -1){
			height = image.getImageData().height;
		}
		if(width == -1){
			width = image.getImageData().width;
		}
		Image imageNew = new Image(Display.getCurrent(), width, height);
		GC gc = new GC(imageNew); 
		gc.drawImage(image, 0, 0, image.getImageData().width, image.getImageData().height, 0, 0, width, height);
		gc.dispose();
		image.dispose();//原图片资源释
		return imageNew;
	}
	
	public static Display display(){
		return Display.getCurrent();
	}
	/**
	 * 获取网络上的图片
	 * */
	public static Image getNetJPG(String u){
		InputStream in = getNetJPGStream(u);
		Image image = new Image(display(), in);
		return image;
	}
	/**
	 * 获取jpg的流
	 * */
	public static InputStream getNetJPGStream(String u){
		URL url;
		InputStream inStream = null;
		try {
			url = new URL(u);

			// 打开链接
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			// 设置请求方式�?GET"
			conn.setRequestMethod("GET");
			// 超时响应时间�?�?
			conn.setConnectTimeout(5 * 1000);
			// 通过输入流获取图片数�?
			inStream = conn.getInputStream();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return inStream;
	}
	/**
	 * 将流保存成图片,存到本地
	 * */
	public static void inputToNative(InputStream in ,String savePath){
		int format = checkPicFormat(savePath);
		ImageData data = null;
		Image image = new Image(display(), in);
		data = image.getImageData();
		image.dispose();
//		try {
////			in.close();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		ImageLoader l = new ImageLoader();
		l.data = new ImageData[]{data};
		l.save(savePath, format);
	}
	/**
	 * 将网络图片存到本地
	 * */
	public static void netToNative(String netPath,String savePath,int[] size,int format){
		if(format == 0){
			format = checkPicFormat(netPath);
		}
		ImageData data = null;
		if(hm.get(netPath) == null){
			InputStream in = getNetJPGStream(netPath);
			Image image = new Image(display(), in);
			data = image.getImageData();
			image.dispose();
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			hm.put(netPath, data);
		}else{
			data = hm.get(netPath);
		}
		if(size!=null){
			int width = data.width;
			int height = data.height;
			if(size[0] == 0){
				size[0] = width;
			}
			if(size[1] == 1){
				size[1] = height;
			}
			data = data.scaledTo(size[0], size[1]);
		}
		ImageLoader l = new ImageLoader();
		l.data = new ImageData[]{data};
		l.save(savePath, format);
		
		
	}
	/**
	 * 判断图片格式
	 * */
	public static int checkPicFormat(String path){
		if(path.length() == 0)return 0;
		int format = 0;
		String[] t = path.split("\\.");
		String formatStr = t[t.length - 1];
		if("jpeg".equals(formatStr)){
			format = SWT.IMAGE_JPEG;
		}else if("jpg".equals(formatStr)){
			format = SWT.IMAGE_JPEG;
		}else if("png".equals(formatStr)){
			format = SWT.IMAGE_PNG;
		}else if("bmp".equals(formatStr)){
			format = SWT.IMAGE_BMP;
		}else if("gif".equals(formatStr)){
			format = SWT.IMAGE_GIF;
		}else if("icon".equals(formatStr)){
			format = SWT.IMAGE_ICO;
		}
		return format;
	}
	
}


 

import java.io.File;
import java.io.InputStream;
import java.util.Date;

public class Simple {
	public final static String IMAGE = System.getProperty("user.dir")+File.separatorChar+"img"+ File.separatorChar;
	public final static String NET_PATH = "http://imgsrc.baidu.com/forum/w%3D580/sign=7ca61064472309f7e76fad1a420c0c39/4eb24a2442a7d933fb79e183ac4bd11372f00169.jpg";
	public final static String NET_TO_NATIVE_PATH = IMAGE + "net_to_native.jpg";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		createFile(IMAGE,false);
		
		String netPath = NET_PATH;
		InputStream in = ImageFoctory.getNetJPGStream(netPath);//获取网络的图片
		String path = NET_TO_NATIVE_PATH;
		ImageFoctory.inputToNative(in, path);//保存到本地
		/**记录时间开始用来比较*/
		long t1 = new Date().getTime();
		ImageFoctory.getImg(path);
		long t2 = new Date().getTime();
		ImageFoctory.getImg(path);
		long t3 = new Date().getTime();
		ImageFoctory.getImg(path);
		long t4 = new Date().getTime();
		
		System.out.println("第 1 次读取图片时,消耗:        "+(t2 - t1)+"     毫秒");
		System.out.println("第 2 次读取图片时,消耗:        "+(t3 - t2)+"     毫秒");
		System.out.println("第 3 次读取图片时,消耗:        "+(t4 - t3)+"     毫秒");
		System.err.println("完成----------------");

	}
	
	public static void createFile(String path, boolean cover) {
		createFile(new File(path), cover);

	}
	/**
	 * 创建一个文件夹
	 * */
	public static  void createFile(File f, boolean cover) {
		if (cover) {
			System.out.println("fileUtil:覆盖。。" + f.getPath());
			f.mkdirs();
			return;
		}
		if (!f.exists()) {// 不存在则创建
			System.out.println("fileUtil:创建。。" + f.getPath());
			f.mkdirs();
		}
	}

}

 下附全套源码。。

如果有什么bug请留言,我会尽快修正,谢谢

 

你可能感兴趣的:(image,bitmapdata)