Android图像处理工具类

本文详细介绍了图片上传、下载及处理工具类的功能实现,包括图片从SD卡读取、保存到SD卡,以及图片转为Base64字符串的流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

还是最近整理代码、、发现了一个师兄写的图片工具类,感觉还是蛮有用的、、主要是图片上传和下载用的比较多吧/*

 * version date author 
 * ────────────────────────────────── 
 * 1.0 2010-9-17 Neal Miao 
 */
package com.winfar.ic.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.kobjects.base64.Base64;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * 图片处理工具类
 * 
 * @author Neal Miao
 * @version
 * @since v 1.0
 * @Date 2011 May 24, 2011 7:24:50 PM
 * 
 * @see
 */
public class ImageUtil {

	/**
	 * 从SD卡里面读取图片
	 *
	 * @param fileName
	 * @return
	 * @return Bitmap
	 * @since v 1.0
	 */
	public static Bitmap getBitmapByPath(String fileName) {
		String myJpgPath = "/sdcard/ic_tmp/" + fileName;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 12;
		Bitmap bm = BitmapFactory.decodeFile(myJpgPath, options);
		return bm;
	}

	/**
	 * 保存图片到SD卡
	 *
	 * @param bitName
	 * @param mBitmap
	 * @throws IOException
	 * @return void
	 * @since v 1.0
	 */
	public static void saveMyBitmap(String bitName, Bitmap mBitmap)
			throws IOException {
		File tmp = new File("/sdcard/ic_tmp/");
		if (!tmp.exists()) {
			tmp.mkdir();
		}
		File f = new File("/sdcard/ic_tmp/" + bitName + ".png");
		f.createNewFile();
		FileOutputStream fOut = null;
		try {
			fOut = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
		try {
			fOut.flush();
			fOut.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String image2Bytes(String imageName) {
		File tmp = new File("/sdcard/ic_tmp/");
		if (!tmp.exists()) {
			return "";
		}
		int bufferSize = 1024;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			File f = new File("/sdcard/ic_tmp/" + imageName);
			FileInputStream fis = new FileInputStream(f);
			BufferedInputStream bis = new BufferedInputStream(fis);
			int ch;
			int i = 0;
			while ((ch = bis.read()) != -1) {
				baos.write(ch);
				if (i++ == bufferSize) {
					baos.flush();
					i = 0;
				}
			}
			baos.flush(); // 提交文件流,很关键
			bis.close();
		} catch (FileNotFoundException e) {
			MyLog.e("ImageUtil", e.getMessage());
			return "";
		} catch (IOException e) {
			MyLog.e("ImageUtil", e.getMessage());
			return "";
		}
		return Base64.encode(baos.toByteArray());
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值