Android图片处理工具类BitmapUtils

 更新时间:2017年12月25日 09:18:55   作者:吕冲  
这篇文章主要为大家详细介绍了Android图片的处理工具类BitmapUtils,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Android图片的处理工具类BitmapUtils,供大家参考,具体内容如下

项目中经常会用到图片,所以在这先简单的总结一下。闲言少叙,上代码。

package com.lvstudio.myapp.utils;


import android.content.Context;
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.util.DisplayMetrics;
import android.view.WindowManager;

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;

/**
 * Created by LvStudio on 2016/11/7.
 */

public class BitmapUtils {

  /**
   * 屏幕分辨率和指定清晰度的图片压缩方法
   *
   * @param context
   * @param image  Bitmap图片
   * @return
   */
  public static Bitmap comp(Context context, Bitmap image) {
    int maxLength = 1024 * 1024; // 预定的图片最大内存,单位byte
    // 压缩大小
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
    int options = 100;
    while (baos.toByteArray().length > maxLength) { // 循环判断,大于继续压缩
      options -= 10;// 每次都减少10
      baos.reset();// 重置baos即清空baos
      image.compress(Bitmap.CompressFormat.JPEG, options, baos);//PNG 压缩options%
    }
    // 压缩尺寸
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    BitmapFactory.Options opts = new BitmapFactory.Options(); // 选项对象(在加载图片时使用)
    opts.inJustDecodeBounds = true; // 修改选项, 只获取大小
    BitmapFactory.decodeStream(bais, null, opts);// 加载图片(只得到图片大小)
    // 获取屏幕大小,按比例压缩
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int scaleX = opts.outWidth / manager.getDefaultDisplay().getWidth(); // X轴缩放比例(图片宽度/屏幕宽度)
    int scaleY = opts.outHeight / manager.getDefaultDisplay().getHeight(); // Y轴缩放比例
    int scale = scaleX > scaleY ? scaleX : scaleY; // 图片的缩放比例(X和Y哪个大选哪个)

    opts.inJustDecodeBounds = false; // 修改选项, 不只解码边界
    opts.inSampleSize = scale > 1 ? scale : 1; // 修改选项, 加载图片时的缩放比例
    return BitmapFactory.decodeStream(bais, null, opts); // 加载图片(得到压缩后的图片)
  }

  /**
   * 屏幕分辨率和指定清晰度的图片压缩方法
   *
   * @param context
   * @param path  图片的路径
   * @return
   */
  public static Bitmap comp(Context context, String path) {
    return compressImage(getUsableImage(context, path));
  }

  /**
   * 获取屏幕分辨率的Bitmap
   *
   * @param context
   * @param path  图片的路径
   * @return
   */
  public static Bitmap getUsableImage(Context context, String path) {
    BitmapFactory.Options opts = new BitmapFactory.Options(); // 选项对象(在加载图片时使用)
    opts.inJustDecodeBounds = true; // 修改选项, 只获取大小
    BitmapFactory.decodeFile(path, opts); // 加载图片(只得到图片大小)
    DisplayMetrics metrics = new DisplayMetrics();
    metrics = context.getApplicationContext().getResources().getDisplayMetrics();
    int scaleX = opts.outWidth / metrics.widthPixels; // X轴缩放比例(图片宽度/屏幕宽度)
    int scaleY = opts.outHeight / metrics.heightPixels; // Y轴缩放比例
    int scale = scaleX > scaleY ? scaleX : scaleY; // 图片的缩放比例(X和Y哪个大选哪个)

    opts.inJustDecodeBounds = false; // 修改选项, 不只解码边界
    opts.inSampleSize = scale > 1 ? scale : 1; // 修改选项, 加载图片时的缩放比例
    return BitmapFactory.decodeFile(path, opts); // 加载图片(得到缩放后的图片)
  }

  /**
   * 压缩图片清晰度,到指定大小
   *
   * @param image
   * @return
   */
  public static Bitmap compressImage(Bitmap image) {
    int maxLength = 1024 * 1024; // (byte)

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
    int options = 100;
    while (baos.toByteArray().length > maxLength) { // 循环判断如果压缩后图片是否大于1mb,大于继续压缩
      options -= 10;// 每次都减少10
      baos.reset();// 重置baos即清空baos
      image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
    return bitmap;
  }

  /**
   * 指定分辨率和清晰度的图片压缩方法
   *
   * @param fromFile
   * @param toFile
   * @param reqWidth
   * @param reqHeight
   * @param quality
   */
  public static void transImage(String fromFile, String toFile, int reqWidth, int reqHeight, int quality) {
    Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
    int bitmapWidth = bitmap.getWidth();
    int bitmapHeight = bitmap.getHeight();
    // 缩放的尺寸
    float scaleWidth = (float) reqWidth / bitmapWidth;
    float scaleHeight = (float) reqHeight / bitmapHeight;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    // 产生缩放后的Bitmap对象
    Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);
    // 保存到文件
    bitmap2File(toFile, quality, resizeBitmap);
    if (!bitmap.isRecycled()) {
      // 释放资源,以防止OOM
      bitmap.recycle();
    }
    if (!resizeBitmap.isRecycled()) {
      resizeBitmap.recycle();
    }
  }


  /**
   * Bitmap转换为文件
   *
   * @param toFile
   * @param quality
   * @param bitmap
   * @return
   */
  public static File bitmap2File(String toFile, int quality, Bitmap bitmap) {
    File captureFile = new File(toFile);
    FileOutputStream out = null;
    try {
      out = new FileOutputStream(captureFile);
      if (bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)) {
        out.flush();
        out.close();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return captureFile;
  }

  /**
   * Drawable转换为Bitmap
   *
   * @param drawable
   * @return
   */
  public static Bitmap drawableToBitamp(Drawable drawable) {
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
        : Bitmap.Config.RGB_565;
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    // 注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
  }

  // Bitmap、Drawable、InputStream、byte[] 之间转换

  /**********************************************************/
  // 1. Bitmap to InputStream
  public static InputStream bitmap2Input(Bitmap bitmap, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
    return new ByteArrayInputStream(baos.toByteArray());
  }

  public static InputStream bitmap2Input(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return new ByteArrayInputStream(baos.toByteArray());
  }

  // 2. Bitmap to byte[]
  public static byte[] bitmap2ByteArray(Bitmap bitmap, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
    return baos.toByteArray();
  }

  public static byte[] bitmap2ByteArray(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
  }

  // 3. Drawable to byte[]
  public static byte[] drawable2ByteArray(Drawable drawable) {
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    return out.toByteArray();
  }

  // 4. byte[] to Bitmap
  public static Bitmap byteArray2Bitmap(byte[] bytes) {
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android中导航组件Navigation的实现原理

    Android中导航组件Navigation的实现原理

    大家好,本篇文章主要讲的是Android中导航组件Navigation的实现原理,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02
  • Android开发实现布局中为控件添加选择器的方法

    Android开发实现布局中为控件添加选择器的方法

    这篇文章主要介绍了Android开发实现布局中为控件添加选择器的方法,涉及Android开发中布局设置的相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • Android弹出窗口实现方法

    Android弹出窗口实现方法

    这篇文章主要介绍了Android弹出窗口实现方法,涉及Android TextView及鼠标事件的响应相关技巧,需要的朋友可以参考下
    2016-01-01
  • Android BroadcastReceiver常见监听整理

    Android BroadcastReceiver常见监听整理

    这篇文章主要介绍了Android BroadcastReceiver常见监听整理的相关资料,需要的朋友可以参考下
    2016-10-10
  • android 实现侧边弹窗特效代码

    android 实现侧边弹窗特效代码

    侧边弹窗是在左边,需要定位好位置,实现原理其实就是进出动效,用位移加透明度效果来控制,下面通过代码给大家介绍android 实现侧边弹窗,需要的朋友参考下吧
    2021-06-06
  • Android 界面开发颜色整理

    Android 界面开发颜色整理

    本文主要介绍Android 界面开发的颜色,这里整理了很多颜色以供大家参考,希望Android 开发的工作者可以参考使用
    2016-07-07
  • Android中简单的电话管理与短信管理App编写实例

    Android中简单的电话管理与短信管理App编写实例

    这篇文章主要介绍了Android中简单的电话管理与短信管理App编写实例,包括监听电话的呼叫状态以及短信群发联系人选择等基本功能的实现,代码突出要点,需要的朋友可以参考下
    2016-04-04
  • 一些有效的Android启动优化策略分享

    一些有效的Android启动优化策略分享

    在当今激烈竞争的移动应用市场,应用的启动速度直接影响着用户的第一印象和满意度,Android的启动优化是开发者必须关注的关键领域,本文将详细介绍一些强大有效的Android启动优化策略,帮助你优化应用的启动过程,为用户创造更出色的体验,需要的朋友可以参考下
    2023-08-08
  • Android APP数字解锁实例详解

    Android APP数字解锁实例详解

    这篇文章主要介绍了Android 应用数字解锁实例详解的相关资料,这里附有实例代码及实现效果图,需要的朋友可以参考下
    2016-11-11
  • Android开发实现判断通知栏是否打开及前往设置页面的方法

    Android开发实现判断通知栏是否打开及前往设置页面的方法

    这篇文章主要介绍了Android开发实现判断通知栏是否打开及前往设置页面的方法,涉及Android通知栏的打开、判断、设置等相关操作技巧,需要的朋友可以参考下
    2018-01-01

最新评论