欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

非常实用的Android图片工具类

程序员文章站 2022-10-10 11:50:17
本文实例为大家分享了android图片工具类的具体代码,供大家参考,具体内容如下 import java.io.bytearrayoutputstream; i...

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

import java.io.bytearrayoutputstream;
import java.io.file;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
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.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;

/**
 * 图片工具类
 * @author wwj
 * 2013/7/3
 */
public class imageutil {
  /**图片的八个位置**/
  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;  //右下
  
  /**
   * 图像的放大缩小方法
   * @param src    源位图对象
   * @param scalex  宽度比例系数
   * @param scaley  高度比例系数
   * @return 返回位图对象
   */
  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;
  }
  
  /**
   * 图像放大缩小--根据宽度和高度
   * @param src
   * @param width
   * @param height
   * @return
   */
  public static bitmap zoombimtap(bitmap src, int width, int height) {
    return bitmap.createscaledbitmap(src, width, height, true);
  }
  
  /**
   * 将drawable转为bitmap对象
   * @param drawable
   * @return
   */
  public static bitmap drawabletobitmap(drawable drawable) {
    return ((bitmapdrawable)drawable).getbitmap();
  }
  
  
  /**
   * 将bitmap转换为drawable对象
   * @param bitmap
   * @return
   */
  public static drawable bitmaptodrawable(bitmap bitmap) {
    drawable drawable = new bitmapdrawable(bitmap);
    return drawable;
  }
  
  /**
   * bitmap转byte[]
   * @param bitmap
   * @return
   */
  public static byte[] bitmaptobyte(bitmap bitmap) {
    bytearrayoutputstream out = new bytearrayoutputstream();
    bitmap.compress(bitmap.compressformat.png, 100, out);
    return out.tobytearray();
  }
  
  /**
   * byte[]转bitmap
   * @param data
   * @return
   */
  public static bitmap bytetobitmap(byte[] data) {
    if(data.length != 0) {
      return bitmapfactory.decodebytearray(data, 0, data.length);
    }
    return null;
  }
  
  /**
   * 绘制带圆角的图像
   * @param src
   * @param radius
   * @return
   */
  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.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;
  }
  
  /**
   * 创建选中带提示图片
   * @param context
   * @param srcid
   * @param tipid
   * @return
   */
  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);
  }
  
  /**
   * 带倒影的图像
   * @param src
   * @return
   */
  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;
  }
  
  
  /**
   * 独立的倒影图像
   * @param src
   * @return
   */
  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 creategreybitmap(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;
  }
  
  /**
   * 保存图片
   * @param src
   * @param filepath
   * @param format:[bitmap.compressformat.png,bitmap.compressformat.jpeg]
   * @return
   */
  public static boolean saveimage(bitmap src, string filepath, compressformat format) {
    boolean rs = false;
    file file = new file(filepath);
    try {
      fileoutputstream out = new fileoutputstream(file);
      if(src.compress(format, 100, out)) {
        out.flush();  //写入流
      }
      out.close();
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    }
    return rs;
  }
  
  /**
   * 添加水印效果
   * @param src    源位图
   * @param watermark  水印
   * @param direction  方向
   * @param spacing 间距
   * @return
   */
  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;
  }
  
  
  /**
   * 合成图像
   * @param direction
   * @param bitmaps
   * @return
   */
  public static bitmap composebitmap(int direction, bitmap... bitmaps) {
    if(bitmaps.length < 2) {
      return null;
    }
    bitmap firstbitmap = bitmaps[0];
    for (int i = 0; i < bitmaps.length; i++) {
      firstbitmap = composebitmap(firstbitmap, bitmaps[i], direction);
    }
    return firstbitmap;
  }

  /**
   * 合成两张图像
   * @param firstbitmap
   * @param secondbitmap
   * @param direction
   * @return
   */
  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;
  }
  
  
}

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