import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.text.TextUtils; import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; /**
* Created by xiaoxin on 2016/12/23.
*/ public class ImageUtils { /**
* 获取缩放后的本地图片 从文件读取方式二 效率高于方式一
*
* @param filePath 文件路径
* @param width 宽
* @param height 高
* @return
*/
public static Bitmap readBitmapFromFileDescriptor(String filePath, int width, int height) {
try {
FileInputStream fis = new FileInputStream(filePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1; if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height);
} else {
inSampleSize = Math.round(srcWidth / width);
}
} options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize; return BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options);
} catch (Exception ex) {
}
return null;
} /**
* 从输入流中读取文件获取缩放后的本地图片 * @param ins 输入流
* @param width 宽
* @param height 高
* @return
*/
public static Bitmap readBitmapFromInputStream(InputStream ins, int width, int height) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(ins, null, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1; if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height);
} else {
inSampleSize = Math.round(srcWidth / width);
}
} options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize; return BitmapFactory.decodeStream(ins, null, options);
} /**
* 从资源文件中读取文件
*
* @param resources
* @param resourcesId
* @param width
* @param height
* @return
*/ public static Bitmap readBitmapFromResource(Resources resources, int resourcesId, int width, int height) {
InputStream ins = resources.openRawResource(resourcesId);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(ins, null, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1; if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height);
} else {
inSampleSize = Math.round(srcWidth / width);
}
} options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize; return BitmapFactory.decodeStream(ins, null, options);
} /**
* 从二进制数据读取图片
*
* @param data
* @param width
* @param height
* @return
*/
public static Bitmap readBitmapFromByteArray(byte[] data, int width, int height) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1; if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height);
} else {
inSampleSize = Math.round(srcWidth / width);
}
} options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize; return BitmapFactory.decodeByteArray(data, 0, data.length, options);
} /**
* 从assets文件读取图片
*
* @param filePath 文件路径
* @return
*/
public static Bitmap readBitmapFromAssetsFile(Context context, String filePath) {
Bitmap image = null;
AssetManager am = context.getResources().getAssets();
try {
InputStream is = am.open(filePath);
image = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return image;
} /**
* 图片保存文件:
*
* @param filePath
* @param b
* @param quality
*/
public static void writeBitmapToFile(String filePath, Bitmap b, int quality) {
try {
File desFile = new File(filePath);
FileOutputStream fos = new FileOutputStream(desFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
b.compress(Bitmap.CompressFormat.JPEG, quality, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 图片压缩:
*
* @param image
* @return
*/
private static Bitmap compressImage(Bitmap image) {
if (image == null) {
return null;
}
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bytes = baos.toByteArray();
ByteArrayInputStream isBm = new ByteArrayInputStream(bytes);
Bitmap bitmap = BitmapFactory.decodeStream(isBm);
return bitmap;
} catch (OutOfMemoryError e) {
} finally {
try {
if (baos != null) {
baos.close();
}
} catch (IOException e) {
}
}
return null;
} /**
* 根据scale生成一张图片 图片缩放:
*
* @param bitmap
* @param scale 等比缩放值
* @return
*/
public static Bitmap bitmapScale(Bitmap bitmap, float scale) {
Matrix matrix = new Matrix();
matrix.postScale(scale, scale); // 长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizeBmp;
} /**
* 获取图片旋转角度:
* 读取照片exif信息中的旋转角度
*
* @param path 照片路径
* @return角度
*/
private static int readPictureDegree(String path) {
if (TextUtils.isEmpty(path)) {
return 0;
}
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;
}
} catch (Exception e) {
}
return degree;
} /**
* 图片旋转角度:
*
* @param b
* @param rotateDegree
* @return
*/
private static Bitmap rotateBitmap(Bitmap b, float rotateDegree) {
if (b == null) {
return null;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotateDegree);
Bitmap rotaBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
return rotaBitmap;
} /**
* 图片转二进制:
*
* @param bm
* @return
*/
public byte[] bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
} /**
* Bitmap转Drawable
*
* @param resources
* @param bm
* @return
*/
public static Drawable bitmapToDrawable(Resources resources, Bitmap bm) {
Drawable drawable = new BitmapDrawable(resources, bm);
return drawable;
} /**
* Drawable转Bitmap
*
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
}

常用的图片相关方法,读取,保存,压缩,缩放,旋转,drawable转化的更多相关文章

  1. python图片的读取保存

    #coding:utf-8 from PIL import Image import matplotlib.pyplot as plt img=Image.open("F:\\Upan\\源 ...

  2. java 图片压缩 缩放

    废话不多说,直接上代码,静态方法可直接调用,中间用流来处理的 /** * 图片缩放(未考虑多种图片格式和等比例缩放) * @param filePath 图片路径 * @param height 高度 ...

  3. Java图片缩略图裁剪水印缩放旋转压缩转格式-Thumbnailator图像处理

    前言 java开发中经常遇到对图片的处理,JDK中也提供了对应的工具类,不过处理起来很麻烦,Thumbnailator是一个优秀的图片处理的开源Java类库,处理效果远比Java API的好,从API ...

  4. 图片纯前端JS压缩的实现

    一.图片上传前端压缩的现实意义 对于大尺寸图片的上传,在前端进行压缩除了省流量外,最大的意义是极大的提高了用户体验. 这种体验包括两方面: 由于上传图片尺寸比较小,因此上传速度会比较快,交互会更加流畅 ...

  5. vue下实现input实现图片上传,压缩,拼接以及旋转

    背景 作为一名前端工作人员,相信大家在开发系统的时候,经常有遇到需要这么一种需求,就是需要为用户保存上传的图片,很多小白遇到这个问题的时候,都会虎躯一震,以为会是一个棘手的问题,当你读完这篇文章的时候 ...

  6. 聊一聊几种常用web图片格式:gif、jpg、png、webp

    前言 在大多数的web页面中,图片占到了页面大小的60%-70%.因此在web开发中,不同的场景使用合适的图片格式对web页面的性能和体验是很重要的.图片格式种类非常多,本文仅针对几种web应用中常用 ...

  7. 压缩图片工具类,压缩100KB以内拿走直接用

    最近遇到自拍上传图片过大问题,很烦恼,所以自己写了一个压缩图片的工具类使用,自测效果很不错,可以压缩到KB以内,像素还可以分辨清晰 下面Java代码奉上: import lombok.extern.s ...

  8. Android微信分享图片大于32k进行压缩

    微信分享视频的时候,需要传一个图片数组,大小不能大于32k. 解决方案:使用Bitmap自带的compress方法解决了这个问题. 源码如下: <span style="font-si ...

  9. php图片水印添加,压缩,剪切的封装类

    php对图片文件的操作主要是利用GD库扩展.当我们频繁利用php对图片进行操作时,会自然封装很多函数,否则会写太多重复的代码.当有很多对图片的相关函数的时候,我们可以考虑将这些函数也整理一下,因而就有 ...

随机推荐

  1. python3+selenium使用浏览器IE的时候,无法打开IE浏览器,老是报错: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones

    python3+selenium使用浏览器IE的时候,老是报错: Unexpected error launching Internet Explorer. Protected Mode settin ...

  2. 性能测试之Jmeter学习(八)

    本节主要学习:断言 JMeter也有像LR中的检查点,本节就来介绍下JMeter的检查点如何去实现. JMeter里面的检查点通过添加断言来完成. 检查点:上一节讲到,我们对用户名和密码进行了参数化, ...

  3. day10servlet编程

    Servlet学习的大纲:  . servlet概念及相关接口简介  . servet 执行过程  . servlet路径映射  . 缺省servlet --应用  . servlet生命周 ...

  4. 25.ProfileService实现(调试)

    上一节课拿到的AccessToken和IdToken 实现ProfileService类 在服务端 添加ProfileService类 需要继承IProfileServiuce 用到的画图工具 Ipr ...

  5. 7.12实习培训日志 Linux Docker

    Linux 管理 RHEL7 的用户和组 用户的属性修改 chage -l [username] #查看用户信息 usermod --expiredate=YYYY-MM-DD [username] ...

  6. 自定义TabWidget

    在开发过程中,默认的TabWidget不能满足我们对于UI的要求并且没有足够的属性工我们去修改,这个时候能够自定义TabWidget是非常必要的.自定义TabWidget组要运用的是TabSpec.s ...

  7. python接口测试框架遇到的坑(一)excel数字转文本

    一.遇到的问题 python编写接口测试框架中,接口用例使用excel维护,其中预期值(code码的值)20000和实际值总是不一致,后来通过打印type发现一个是unicode,一个是float. ...

  8. [转]监控常用TCODE

    1  系统监视 1.1 进程监视 SM66/SM50 进程查看 管理员需全天监看系统的进程.长时间运行的后台工作,有缺陷的报表程序,若不进行控制都将消耗掉大量的系统资源.管理员用这个事务码检查他们的环 ...

  9. 用户唯一性验证(ajax)

    验证用户添加或者修改时用户名的唯一性: 验证时机:用户名改变时,表单提交时. 1.jsp页面:(前端) <%@ page contentType="text/html;charset= ...

  10. PJzhang:一站式跨境卖家网址导航Amz520.com

    猫宁!!! 参考链接:http://www.guxiaobei.com/amz520-release.html www.amz520.com是一个跨境电商导航站点,汇集了大量的高效信息,做这个站点花费 ...