• Bitmap工具类


    一直在使用的一个Bitmap工具类

    处理Bitmap和ImageView对象,实现了下面功能:

    1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存.
    2.getViewBitmap: 从view得到bitmap对象
    3.addWatermark: Bitmap加水印
    4.zoomBitmap: 放大缩小图片
    5.getLoacalBitmap: 传入路径,从持久存储(SD卡或手机内存)得到Bitmap对象
    6.getBitMapByUrl: 通过URL地址获取Bitmap对象

    7.toGrayscale: 对Bitmap进行滤镜特效处理.实现了图片变黑白,变亮,变暗,高对照,低对照,高饱和,低饱和

    下载地址:http://download.csdn.net/download/landehuxi/6661713

    源代码例如以下:

    public class ImgService {
    	//亮
    	public  final float[] LIGHT_ARR = new float[] { 
    			1, 0, 0, 0, 100, 
    			0, 1, 0, 0, 100, 
    			0, 0, 1, 0, 100,
    			0, 0, 0, 1, 0 };
    	//暗
    	public  final float[] DARK_ARR = new float[] { 
    			0.2f, 0, 0, 0, 50.8f, 
    			0, 0.2f, 0, 0, 50.8f, 
    			0, 0,0.2f, 0, 50.8f, 
    			0, 0, 0, 1f, 0 };
    	//高对照
    	public  final float[] GDB_ARR = new float[] { 
    				5, 0, 0, 0, -250, 
    				0, 5, 0, 0, -250, 
    				0, 0, 5, 0, -250,
    				0, 0, 0, 1, 0 };
    	//高对照
    	public  final float[] DDB_ARR = new float[] { 
    				0.2f, 0, 0, 0, 50, 
    				0, 0.2f, 0, 0, 50, 
    				0, 0, 0.2f, 0, 50,
    				0, 0, 0, 1, 0 };
    	//高饱和
    	public  final float[] GBH_ARR = new float[] { 
    				3f, -1.8f, -0.25f, 0, 50, 
    				-0.9f, 2.1f, -0.25f, 0, 50, 
    				-0.9f, -1.8f, 3.8f, 0, 50,
    				0, 0, 0, 1, 0 };
    	//低饱和
    	public final float[] DBH_ARR = new float[] { 
    			0.3f, 0.6f, 0.08f, 0, 0, 
    			0.3f, 0.6f, 0.08f, 0, 0, 
    			0.3f, 0.6f, 0.08f, 0, 0,
    			0, 0, 0, 1, 0 };
    	//COPY
    	public final float[] COPY_ARR = new float[] { 
    				0, 0, 0, 0, 0, 
    				0, 0, 0, 0, 0, 
    				0, 0, 0, 0, 0,
    				0, 0, 0, 0, 0 };
    	/**
    	 * 为图片加滤镜特效.array參数为ImgService定义的几个滤镜矩阵.如ImgService.LIGHT_ARR
    	 * @param bmpOriginal
    	 * @param array
    	 * @return
    	 */
    	public Bitmap toGrayscale(Bitmap bmpOriginal, float[] array) {
    		int width, height;
    		height = bmpOriginal.getHeight();
    		width = bmpOriginal.getWidth();
    
    		Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
    				Bitmap.Config.RGB_565);
    		Canvas c = new Canvas(bmpGrayscale);
    		Paint paint = new Paint();
    		ColorMatrix colorMatrix = new ColorMatrix();
    		colorMatrix.set(array);
    		paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
    		c.drawBitmap(bmpOriginal, 0, 0, paint);
    		bmpOriginal.recycle();
    		bmpOriginal=null;
    		return bmpGrayscale;
    	}
    
    	public boolean saveBitmap(Bitmap bitmap, String fileName, String path) {
    		File file = new File(path);
    		FileOutputStream fos=null;
    		if (!file.exists()) {
    			file.mkdir();
    		}
    		File imageFile = new File(file, fileName);
    		try {
    			imageFile.createNewFile();
    			fos = new FileOutputStream(imageFile);
    			bitmap.compress(CompressFormat.JPEG, 50, fos);
    			fos.flush();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}finally{
    			if(fos!=null){
    				try {
    					fos.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    				fos=null;
    			}
    		}
    		return true;
    	}
    
    	// 从view得到bitmap
    	public Bitmap getViewBitmap(View view) {
    		Bitmap bitmap = null;
    		try {
    			int width = view.getWidth();
    			int height = view.getHeight();
    			if (width != 0 && height != 0) {
    				bitmap = Bitmap.createBitmap(width, height,
    						Bitmap.Config.ARGB_8888);
    				Canvas canvas = new Canvas(bitmap);
    				view.draw(canvas);
    			}
    		} catch (Exception e) {
    			bitmap = null;
    			Debug.out(e);
    		}
    		return bitmap;
    	}
    
    	// Bitmap加水印
    	public Bitmap addWatermark(Bitmap src, Bitmap watermark) {
    		if (src == null || watermark == null) {
    			return src;
    		}
    
    		int sWid = src.getWidth();
    		int sHei = src.getHeight();
    		int wWid = watermark.getWidth();
    		int wHei = watermark.getHeight();
    		if (sWid == 0 || sHei == 0) {
    			return null;
    		}
    
    		if (sWid < wWid || sHei < wHei) {
    			return src;
    		}
    
    		Bitmap bitmap = Bitmap.createBitmap(sWid, sHei, Config.ARGB_8888);//Config可改动,改变内存占用
    		try {
    			Canvas cv = new Canvas(bitmap);
    			cv.drawBitmap(src, 0, 0, null);
    			cv.drawBitmap(watermark, sWid - wWid - 5, sHei - wHei - 5, null);
    			cv.save(Canvas.ALL_SAVE_FLAG);
    			cv.restore();
    		} catch (Exception e) {
    			bitmap = null;
    			e.getStackTrace();
    		}finally{
    			src.recycle();
    			src=null;
    			watermark.recycle();
    			watermark=null;
    		}
    		return bitmap;
    	}
    	/**
    	 * 放大缩小图片
    	 * 
    	 * @Title: zoomBitmap
    	 * @param @param bitmap
    	 * @param @param w
    	 * @param @param h
    	 * @return Bitmap
    	 * @throws
    	 */
    	public  Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    		int width = bitmap.getWidth();
    		int height = bitmap.getHeight();
    		Matrix matrix = new Matrix();
    		float scaleWidht = ((float) w / width);
    		float scaleHeight = ((float) h / height);
    		matrix.postScale(scaleWidht, scaleHeight);
    		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    		bitmap.recycle();
    		bitmap=null;
    		return newbmp;
    	}
    	/**
    	 * @Title: getLoacalBitmap
    	 * @Description: 载入本地图片
    	 * @param @param url 本地路径
    	 * @param @return
    	 * @return Bitmap
    	 * @throws
    	 */
    	public  Bitmap getLoacalBitmap(String url) {
    		if (url != null) {
    			FileInputStream fis=null;
    			try {
    				fis = new FileInputStream(url);
    				return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    				return null;
    			}finally{
    				StreamService.close(fis);
    				if(fis!=null){
    					try {
    						fis.close();
    					} catch (IOException e) {
    						e.printStackTrace();
    					}
    					fis=null;
    				}
    			}
    		} else {
    			return null;
    		}
    	}
    
    	/**
    	 * 通过URL地址获取Bitmap对象
    	 * 
    	 * @Title: getBitMapByUrl
    	 * @param @param url
    	 * @param @return
    	 * @param @throws Exception
    	 * @return Bitmap
    	 * @throws
    	 */
    	public  Bitmap getBitMapByUrl(final String url) {
    		URL fileUrl = null;
    		InputStream is=null;
    		Bitmap bitmap = null;
    		try {
    			fileUrl = new URL(url);
    			HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();
    			conn.setDoInput(true);
    			conn.connect();
    			is = conn.getInputStream();
    			bitmap = BitmapFactory.decodeStream(is);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally{
    			try {
    				if (null!=is) {
    					is.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			is=null;
    		}
    		return bitmap;
    	}
    	
    }

  • 相关阅读:
    C#新特性
    蛋清打发奶油状
    VS 2015 开发Android底部导航条----[实例代码,多图]
    使用微软的MSBuild.exe编译VS .sln .csproj 文件
    双色球基础分析--SQL
    Windows 7 中的 God Mode
    Free Online SQL Formatter
    Windows 特殊文件夹
    常用DNS列表(电信、网通)
    C语言词法分析:C#源码
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3906993.html
Copyright © 2020-2023  润新知