处理图片的工具类
来源:互联网 发布:nasa数据查询 编辑:程序博客网 时间:2024/05/17 18:14
package cn.cibn.ott.utils;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.media.ThumbnailUtils;
public class ImageUtils {
public static final int TOP = 0;
public static final int BOTTOM = 1;
public static final int LEFT = 2;
public static final int RIGHT = 3;
public static final int LEFT_TOP = 4;
public static final int LEFT_BOTTOM = 5;
public static final int RIGHT_TOP = 6;
public static final int RIGHT_BOTTOM = 7;
/** * 图像放大缩小-根据宽度和高度的比例系数 * */public static Bitmap zoomBitmap(Bitmap src, float scaleX, float scaleY) { Matrix matrix = new Matrix(); matrix.setScale(scaleX, scaleY); Bitmap t_bitmap = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); return t_bitmap;}/** * 图像放大缩小-根据宽度和高度 * */public static Bitmap zoomBitmap(Bitmap src, int width, int height) { return Bitmap.createScaledBitmap(src, width, height, true);}/** * Drawable转Bitmap * */public static Bitmap drawableToBitmap(Drawable drawable) { return ((BitmapDrawable) drawable).getBitmap();}/** * Bitmap转Drawable * */public static Drawable bitmapToDrawable(Bitmap bitmap) { Drawable drawable = new BitmapDrawable(bitmap); return drawable;}/** * Bitmap转byte[] * */public static byte[] bitmapToByte(Bitmap bitmap) { ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); return out.toByteArray();}public static byte[] bitmapToByte(final Bitmap bmp, final boolean needRecycle) { int i; int j; if (bmp.getHeight() > bmp.getWidth()) { i = bmp.getWidth(); j = bmp.getWidth(); } else { i = bmp.getHeight(); j = bmp.getHeight(); } Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565); Canvas localCanvas = new Canvas(localBitmap); while (true) { localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0, i, j), null); if (needRecycle) bmp.recycle(); ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(); localBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream); localBitmap.recycle(); byte[] arrayOfByte = localByteArrayOutputStream.toByteArray(); try { localByteArrayOutputStream.close(); return arrayOfByte; } catch (Exception e) { // F.out(e); } i = bmp.getHeight(); j = bmp.getHeight(); }}/** * byte[]转Bitmap * */public static Bitmap byteToBitmap(byte[] data) { if (data.length != 0) { return BitmapFactory.decodeByteArray(data, 0, data.length); } return null;}/** * 带圆角的图像 * */public static Bitmap createRoundedCornerBitmap(Bitmap src, int radius) { final int w = src.getWidth(); final int h = src.getHeight(); // 高质量32位图 Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Paint paint = new Paint(); Canvas canvas = new Canvas(bitmap); canvas.drawARGB(0, 0, 0, 0); paint.setColor(0xff424242); // 防止边缘的锯齿 paint.setAntiAlias(true); // 用来对位图进行滤波处理 paint.setFilterBitmap(true); Rect rect = new Rect(0, 0, w, h); RectF rectf = new RectF(rect); // 绘制带圆角的矩形 canvas.drawRoundRect(rectf, radius, radius, paint); // 取两层绘制交集。显示上层 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // 绘制图像 canvas.drawBitmap(src, rect, rect, paint); return bitmap;}/** * 创建选中带提示图片 * */public static Drawable createSelectedTip(Context context, int srcId, int tipId) { Bitmap src = BitmapFactory .decodeResource(context.getResources(), srcId); Bitmap tip = BitmapFactory .decodeResource(context.getResources(), tipId); final int w = src.getWidth(); final int h = src.getHeight(); Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Paint paint = new Paint(); Canvas canvas = new Canvas(bitmap); // 绘制原图 canvas.drawBitmap(src, 0, 0, paint); // 绘制提示图片 canvas.drawBitmap(tip, (w - tip.getWidth()), 0, paint); return bitmapToDrawable(bitmap);}/** * 带倒影的图像 * */public static Bitmap createReflectionBitmap(Bitmap src) { // 两个图像间的空隙 final int spacing = 4; final int w = src.getWidth(); final int h = src.getHeight(); // 绘制高质量32位图 Bitmap bitmap = Bitmap.createBitmap(w, h + h / 2 + spacing, Config.ARGB_8888); // 创建沿X轴的倒影图像 Matrix m = new Matrix(); m.setScale(1, -1); Bitmap t_bitmap = Bitmap.createBitmap(src, 0, h / 2, w, h / 2, m, true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); // 绘制原图像 canvas.drawBitmap(src, 0, 0, paint); // 绘制倒影图像 canvas.drawBitmap(t_bitmap, 0, h + spacing, paint); // 线性渲染-沿Y轴高到低渲染 Shader shader = new LinearGradient(0, h + spacing, 0, h + spacing + h / 2, 0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR); paint.setShader(shader); // 取两层绘制交集。显示下层。 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 绘制渲染倒影的矩形 canvas.drawRect(0, h + spacing, w, h + h / 2 + spacing, paint); return bitmap;}/** * 独立的倒影图像 * */public static Bitmap createReflectionBitmapForSingle(Bitmap src) { final int w = src.getWidth(); final int h = src.getHeight(); // 绘制高质量32位图 Bitmap bitmap = Bitmap.createBitmap(w, h / 2, Config.ARGB_8888); // 创建沿X轴的倒影图像 Matrix m = new Matrix(); m.setScale(1, -1); Bitmap t_bitmap = Bitmap.createBitmap(src, 0, h / 2, w, h / 2, m, true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); // 绘制倒影图像 canvas.drawBitmap(t_bitmap, 0, 0, paint); // 线性渲染-沿Y轴高到低渲染 Shader shader = new LinearGradient(0, 0, 0, h / 2, 0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR); paint.setShader(shader); // 取两层绘制交集。显示下层。 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 绘制渲染倒影的矩形 canvas.drawRect(0, 0, w, h / 2, paint); return bitmap;}/** * 变成灰色图像 * */public static Bitmap createGrayBitmap(Bitmap src) { final int w = src.getWidth(); final int h = src.getHeight(); Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); // 颜色变换的矩阵 ColorMatrix matrix = new ColorMatrix(); // saturation 饱和度值,最小可设为0,此时对应的是灰度图; 为1表示饱和度不变,设置大于1,就显示过饱和 matrix.setSaturation(0); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); paint.setColorFilter(filter); canvas.drawBitmap(src, 0, 0, paint); return bitmap;}/** * 保存图片[format:Bitmap.CompressFormat.PNG,Bitmap.CompressFormat.JPEG] * */public static boolean saveImage(Bitmap src, String filepath, CompressFormat format) { boolean isSuccess = false; File file = new File(filepath); try { FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); if (src.compress(format, 100, bos)) { bos.flush(); fos.flush(); } bos.close(); fos.close(); bos = null; fos = null; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return isSuccess;}/** * 添加水印效果 * */public static Bitmap createWatermark(Bitmap src, Bitmap watermark, int direction, int spacing) { final int w = src.getWidth(); final int h = src.getHeight(); Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(src, 0, 0, null); if (direction == LEFT_TOP) { canvas.drawBitmap(watermark, spacing, spacing, null); } else if (direction == LEFT_BOTTOM) { canvas.drawBitmap(watermark, spacing, h - watermark.getHeight() - spacing, null); } else if (direction == RIGHT_TOP) { canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, spacing, null); } else if (direction == RIGHT_BOTTOM) { canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, h - watermark.getHeight() - spacing, null); } return bitmap;}/** * 合成图像 * */public static Bitmap composeBitmap(int direction, Bitmap... bitmaps) { if (bitmaps.length < 2) { return null; } Bitmap firstBitmap = bitmaps[0]; for (int i = 1; i < bitmaps.length; i++) { firstBitmap = composeBitmap(firstBitmap, bitmaps[i], direction); } return firstBitmap;}private static Bitmap composeBitmap(Bitmap firstBitmap, Bitmap secondBitmap, int direction) { if (firstBitmap == null) { return null; } if (secondBitmap == null) { return firstBitmap; } final int fw = firstBitmap.getWidth(); final int fh = firstBitmap.getHeight(); final int sw = secondBitmap.getWidth(); final int sh = secondBitmap.getHeight(); Bitmap bitmap = null; Canvas canvas = null; if (direction == TOP) { bitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(secondBitmap, 0, 0, null); canvas.drawBitmap(firstBitmap, 0, sh, null); } else if (direction == BOTTOM) { bitmap = Bitmap.createBitmap(fw > sw ? fw : sw, fh + sh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(firstBitmap, 0, 0, null); canvas.drawBitmap(secondBitmap, 0, fh, null); } else if (direction == LEFT) { bitmap = Bitmap.createBitmap(fw + sw, sh > fh ? sh : fh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(secondBitmap, 0, 0, null); canvas.drawBitmap(firstBitmap, sw, 0, null); } else if (direction == RIGHT) { bitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawBitmap(firstBitmap, 0, 0, null); canvas.drawBitmap(secondBitmap, fw, 0, null); } return bitmap;}/** * 读取本地资源的图片 * * @param context * @param resId * @return */public static Bitmap ReadBitmapById(Context context, int resId) { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.ALPHA_8; opt.inPurgeable = true; opt.inInputShareable = true; // 获取资源图片 InputStream is = context.getResources().openRawResource(resId); return BitmapFactory.decodeStream(is, null, opt);}/*** * 根据资源文件获取Bitmap * * @param context * @param drawableId * @return */public static Bitmap ReadBitmapById(Context context, int drawableId, int screenWidth, int screenHight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Config.ARGB_8888; options.inInputShareable = true; options.inPurgeable = true; InputStream stream = context.getResources().openRawResource(drawableId); Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options); return getBitmap(bitmap, screenWidth, screenHight);}/*** * 等比例压缩图片 * * @param bitmap * @param screenWidth * @param screenHight * @return */public static Bitmap getBitmap(Bitmap bitmap, int screenWidth, int screenHight) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scale = (float) screenWidth / w; float scale2 = (float) screenHight / h; // scale = scale < scale2 ? scale : scale2; // 保证图片不变形. matrix.postScale(scale, scale); // w,h是原图的属性. return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);}// 这是第一种压缩算法 ,直接返回bitmap类型的// 质量压缩方法public static Bitmap compressImageByQuality(Bitmap image) { // 图片大小 int size = 100; ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; // LogUtil.i(DebugConfig.DEBUGNAME, "图片的字节数为:" + // baos.toByteArray().length); while (baos.toByteArray().length / 1024 > size) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();// 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 10, baos);// 这里压缩options%,把压缩后的数据存放到baos中 // LogUtil.i(DebugConfig.DEBUGNAME, "图片的字节数为:" // + baos.toByteArray().length); } return image;}// 按比例大小压缩方法public static Bitmap compressImageByProportion(String imagePath, int height, int width) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(imagePath, newOpts);// 此时返回bm为空 newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = height;// 这里设置高度为800f float ww = width;// 这里设置宽度为480f // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;// be=1表示不缩放 if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;// 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 bitmap = BitmapFactory.decodeFile(imagePath, newOpts); return compressImageByQuality(bitmap);// 压缩好比例大小后再进行质量压缩}// 按比例大小压缩图片public static Bitmap compressImageByProportion(Bitmap image, int height, int width) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); if (baos.toByteArray().length / 1024 > 1024) {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 baos.reset();// 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = height;// 这里设置高度为800f float ww = width;// 这里设置宽度为480f // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;// be=1表示不缩放 if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; // 另一种算法 // be = (int) ((w / STANDARD_WIDTH + h/ STANDARD_HEIGHT) / 2); newOpts.inSampleSize = be;// 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 isBm = new ByteArrayInputStream(baos.toByteArray()); bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); return compressImageByQuality(bitmap);// 压缩好比例大小后再进行质量压缩}// 按比例大小压缩图片public static ByteArrayOutputStream compressImageByProportion(Bitmap image, int width, int height, Picture picture) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); if (baos.toByteArray().length / 1024 > 1024) {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 baos.reset();// 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = height;// 这里设置高度为800f float ww = width;// 这里设置宽度为480f // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;// be=1表示不缩放 if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; // 另一种算法 // be = (int) ((w / STANDARD_WIDTH + h/ STANDARD_HEIGHT) / 2); newOpts.inSampleSize = be;// 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 isBm = new ByteArrayInputStream(baos.toByteArray()); bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); baos.reset(); bitmap.compress(Bitmap.CompressFormat.JPEG, 65, baos); bitmap.recycle(); return baos;}/** * 根据指定的图像路径和大小来获取缩略图 此方法有两点好处: 1. * 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度, * 第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。 2. * 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使 用这个工具生成的图像不会被拉伸。 * * @param imagePath * 图像的路径 * @param width * 指定输出图像的宽度 * @param height * 指定输出图像的高度 * @return 生成的缩略图 */public static Bitmap getImageThumbnail(String imagePath, int width, int height) { Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 获取这个图片的宽和高,注意此处的bitmap为null bitmap = BitmapFactory.decodeFile(imagePath, options); options.inJustDecodeBounds = false; // 设为 false // 计算缩放比 int h = options.outHeight; int w = options.outWidth; int beWidth = w / width; int beHeight = h / height; int be = 1; if (beWidth < beHeight) { be = beWidth; } else { be = beHeight; } if (be <= 0) { be = 1; } options.inSampleSize = be; // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false bitmap = BitmapFactory.decodeFile(imagePath, options); // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象 bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap;}/** * 获取视频的缩略图 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。 * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。 * * @param videoPath * 视频的路径 * @param width * 指定输出视频缩略图的宽度 * @param height * 指定输出视频缩略图的高度度 * @param kind * 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。 * 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96 * @return 指定大小的视频缩略图 */public static ByteArrayOutputStream getVideoThumbnail(String videoPath, int width, int height, int kind) { Bitmap bitmap = null; // 获取视频的缩略图 bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind); System.out.println("w" + bitmap.getWidth()); System.out.println("h" + bitmap.getHeight()); bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, baos);// (0-100) bitmap.recycle(); return baos;}/** * 获取视频的缩略图 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。 * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。 * * @param videoPath * 视频的路径 * @param width * 指定输出视频缩略图的宽度 * @param height * 指定输出视频缩略图的高度度 * @param kind * 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。 * 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96 * @return 指定大小的视频缩略图 */public static Bitmap getVideoThumbnailBitmap(String videoPath, int width, int height, int kind) { Bitmap bitmap = null; // 获取视频的缩略图 bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind); System.out.println("w" + bitmap.getWidth()); System.out.println("h" + bitmap.getHeight()); bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, baos);// (0-100) return bitmap;}/** * 读取图片属性:旋转的角度 * * @param path * 图片绝对路径 * @return degree旋转的角度 */public static int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; // case ExifInterface.ORIENTATION_NORMAL: // degree = 90; // break; } } catch (IOException e) { e.printStackTrace(); } return degree;}/** * 旋转图片 * * @param angle * @param bitmap * @return Bitmap */public static Bitmap rotaingImageView(int angle, Bitmap bitmap) { // 旋转图片 动作 Matrix matrix = new Matrix(); matrix.postRotate(angle); // LogUtil.i("rotaingImageView", "angle2=" + angle); // 创建新的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); return resizedBitmap;}public static Bitmap convertToBitmap(String path, int w, int h) { BitmapFactory.Options opts = new BitmapFactory.Options(); // 设置为ture只获取图片大小 opts.inJustDecodeBounds = true; opts.inPreferredConfig = Bitmap.Config.ARGB_8888; // 返回为空 BitmapFactory.decodeFile(path, opts); int width = opts.outWidth; int height = opts.outHeight; float scaleWidth = 0.f, scaleHeight = 0.f; if (width > w || height > h) { // 缩放 scaleWidth = ((float) width) / w; scaleHeight = ((float) height) / h; } opts.inJustDecodeBounds = false; float scale = Math.max(scaleWidth, scaleHeight); opts.inSampleSize = (int) scale; WeakReference<Bitmap> weak = new WeakReference<Bitmap>( BitmapFactory.decodeFile(path, opts)); return Bitmap.createScaledBitmap(weak.get(), w, h, true);}public static void saveBitmap(Bitmap bitmap, String filePath) { File file = new File(filePath); if (file.exists()) { file.delete(); } FileOutputStream out; try { out = new FileOutputStream(file); if (bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out)) { out.flush(); out.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
}
- 处理图片的工具类
- 处理图片的工具类
- 处理图片的工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- android 处理图片的工具类
- Java的图片处理工具类
- Java的图片处理工具类
- Java的图片处理工具类
- MTK图片处理的工具类
- Java的图片处理工具类
- Java的图片处理工具类
- Javascript Dom节点及操作
- jQuery 事件
- 对象作用域与Servlet事件监听器
- jQuery 元素选择器
- ORA-00214 控制文件版本不一致恢复过程
- 处理图片的工具类
- jQuery及AngularJs的ajax请求对比
- Linux下的C语言基础编程——统计字符
- 学习java的路线图-五个必经阶段
- mysql-select查询操作
- bower安装报错”Cannot be run with sudo”解决办法
- 前端编码规范
- 递归思想解决汉诺塔的问题
- 二叉树的深度优先遍历和广度优先遍历