一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Android BitmapUtils工具类使用详解

时间:2022-06-25 22:53:28 编辑:袖梨 来源:一聚教程网

本文实例为大家分享了Android BitmapUtils工具类的具体代码,供大家参考,具体内容如下

public final class BitmapUtils {
  public static final String TAG = "BitmapUtil";
  private static int sShotScreen;
  private static int sShotScreen;
  private static int sShotScreenSize = sShotScreenWidth * sShotScreenHeight;

  @SuppressLint("StaticFieldLeak")
  private static Context mContext;
  @SuppressLint("StaticFieldLeak")
  private static Activity mActivity;

  public void init(Context context,Activity ac) {
    mContext=context;
    mActivity=ac;

    DisplayMetrics dm = new DisplayMetrics();
    ac.getWindowManager().getDefaultDisplay().getMetrics(dm);
    //获取屏幕分辨率
    sShotScreenWidth = dm.widthPixels;
    sShotScreenHeight = dm.heightPixels;
    sShotScreenSize = sShotScreenWidth * sShotScreenHeight;
  }

  /**
   * 图片合成
   * 
   * @param bitmap 位图1
   * @param mark 位图2
   * @return Bitmap
   */
  public static Bitmap createBitmap(Bitmap bitmap, Bitmap mark) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    int mW = mark.getWidth();
    int mH = mark.getHeight();
    Bitmap newbitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个长宽一样的位图

    Canvas cv = new Canvas(newbitmap);
    cv.drawBitmap(bitmap, 0, 0, null);// 在 0,0坐标开始画入bitmap
    cv.drawBitmap(mark, w - mW , h - mH , null);// 在右下角画入水印mark
    cv.save(Canvas.ALL_SAVE_FLAG);// 保存
    cv.restore();// 存储
    return newbitmap;
  }

  /**
   * 放大缩小图片
   * @param bitmap 位图
   * @param w 新的宽度
   * @param h 新的高度
   * @return Bitmap
   */
  public static 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);
    return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
  }

  /**
   * 旋转图片
   * @param bitmap 要旋转的图片
   * @param angle 旋转角度
   * @return bitmap
   */
  public static Bitmap rotate(Bitmap bitmap,int angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
        bitmap.getHeight(), matrix, true);
  }

  /**
   * 圆形图片
   *@param source 位图
   * @param strokeWidth 裁剪范围 0表示最大
   * @param bl 是否需要描边
   * @param bl 画笔粗细
   * @param bl 颜色代码
   * @return bitmap
   */
  public static Bitmap createCircleBitmap(Bitmap source, int strokeWidth, boolean bl,int edge,int color) {

    int diameter = source.getWidth()  1) {
      // 缩放图片 此处用到平方根 将宽带和高度压缩掉对应的平方根倍
      // (保持宽高不变,缩放后也达到了最大占用空间的大小)
      return scaleWithWH(bitmap,bitmap.getWidth() / Math.sqrt(i),
              bitmap.getHeight() / Math.sqrt(i));
    }
    return null;
  }

  /***
   * 图片缩放
   *@param bitmap 位图
   * @param w 新的宽度
   * @param h 新的高度
   * @return Bitmap
   */
  public static Bitmap scaleWithWH(Bitmap bitmap, double w, double h) {
    if (w == 0 || h == 0 || bitmap == null) {
      return bitmap;
    } else {
      int width = bitmap.getWidth();
      int height = bitmap.getHeight();

      Matrix matrix = new Matrix();
      float scaleWidth = (float) (w / width);
      float scaleHeight = (float) (h / height);
      
      matrix.postScale(scaleWidth, scaleHeight);
      return Bitmap.createBitmap(bitmap, 0, 0, width, height,
          matrix, true);
    }
  }

  /**
   * YUV视频流格式转bitmap
   * @param data YUV视频流格式
   * @return width 设置宽度
   * @return width 设置高度
   */
  public static Bitmap getBitmap(byte[] data, int width, int height) {
    Bitmap bitmap;
    YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, width, height, null);
    //data是onPreviewFrame参数提供
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(0, 0, yuvimage.getWidth(), yuvimage.getHeight()), 100, baos);//
    // 80--JPG图片的质量[0-100],100最高
    byte[] rawImage = baos.toByteArray();
    BitmapFactory.Options options = new BitmapFactory.Options();
    SoftReference softRef = new SoftReference(BitmapFactory.decodeByteArray(rawImage, 0, rawImage
        .length, options));
    bitmap = softRef.get();
    return bitmap;
  }

  /**
   * 图片路径转bitmap
   * @param file 图片的绝对路径
   * @return bitmap
   */
  public static Bitmap getAssetImage(String file) {
    Bitmap bitmap = null;
    AssetManager am = mActivity.getAssets();
    try {
      InputStream is = am.open(file);
      bitmap = BitmapFactory.decodeStream(is);
      is.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return bitmap;
  }

  /**
   * bitmap保存到指定路径
   * @param file 图片的绝对路径
   * @param file 位图
   * @return bitmap
   */
  public static boolean saveFile(String file, Bitmap bmp) {
    if(TextUtils.isEmpty(file) || bmp == null) return false;
    
    File f = new File(file);
    if (f.exists()) {
      f.delete();
    }else {
      File p = f.getParentFile();
      if(!p.exists()) {
        p.mkdirs();
      }
    }
    try {
      FileOutputStream out = new FileOutputStream(f);
      bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
      out.flush();
      out.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /**
   * 回收一个未被回收的Bitmap
   *@param bitmap
   */
  public static void doRecycledIfNot(Bitmap bitmap) {
    if (!bitmap.isRecycled()) {
      bitmap.recycle();
    }
  }
}

热门栏目