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

android图片圆角、图片去色处理示例

程序员文章站 2023-08-29 19:15:02
android中图片处理 用来对android中的项目图片进行处理 复制代码 代码如下:package com.zhanggeng.contact.tools; im...

android中图片处理

用来对android中的项目图片进行处理

复制代码 代码如下:

package com.zhanggeng.contact.tools;

import java.io.bytearrayoutputstream;
import java.io.file;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;

import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.bitmapfactory;
import android.graphics.canvas;
import android.graphics.colormatrix;
import android.graphics.colormatrixcolorfilter;
import android.graphics.paint;
import android.graphics.porterduff.mode;
import android.graphics.porterduffxfermode;
import android.graphics.rect;
import android.graphics.rectf;
import android.graphics.drawable.bitmapdrawable;
import android.view.viewgroup;
import android.view.animation.accelerateinterpolator;

/**
 * 处理图片的工具类.
 *
 */
public class imagetools {

 /** */
 /**
  * 图片去色,返回灰度图片
  *
  * @param bmporiginal
  *            传入的图片
  * @return 去色后的图片
  */
 public static bitmap tograyscale(bitmap bmporiginal) {
  int width, height;
  height = bmporiginal.getheight();
  width = bmporiginal.getwidth();
  bitmap bmpgrayscale = bitmap.createbitmap(width, height,
    bitmap.config.rgb_565);
  canvas c = new canvas(bmpgrayscale);
  paint paint = new paint();
  colormatrix cm = new colormatrix();
  cm.setsaturation(0);
  colormatrixcolorfilter f = new colormatrixcolorfilter(cm);
  paint.setcolorfilter(f);
  c.drawbitmap(bmporiginal, 0, 0, paint);
  return bmpgrayscale;
 }

 /** */
 /**
  * 去色同时加圆角
  *
  * @param bmporiginal
  *            原图
  * @param pixels
  *            圆角弧度
  * @return 修改后的图片
  */
 public static bitmap tograyscale(bitmap bmporiginal, int pixels) {
  return toroundcorner(tograyscale(bmporiginal), pixels);
 }

 /** */
 /**
  * 把图片变成圆角
  *
  * @param bitmap
  *            需要修改的图片
  * @param pixels
  *            圆角的弧度
  * @return 圆角图片
  */
 public static bitmap toroundcorner(bitmap bitmap, int pixels) {

  bitmap output = bitmap.createbitmap(bitmap.getwidth(),
    bitmap.getheight(), config.argb_8888);
  canvas canvas = new canvas(output);

  final int color = 0xff424242;
  final paint paint = new paint();
  final rect rect = new rect(0, 0, bitmap.getwidth(), bitmap.getheight());
  final rectf rectf = new rectf(rect);
  final float roundpx = pixels;

  paint.setantialias(true);
  canvas.drawargb(0, 0, 0, 0);
  paint.setcolor(color);
  canvas.drawroundrect(rectf, roundpx, roundpx, paint);

  paint.setxfermode(new porterduffxfermode(mode.src_in));
  canvas.drawbitmap(bitmap, rect, rect, paint);

  return output;
 }

 /** */
 /**
  * 使圆角功能支持bitampdrawable
  *
  * @param bitmapdrawable
  * @param pixels
  * @return
  */
 public static bitmapdrawable toroundcorner(bitmapdrawable bitmapdrawable,
   int pixels) {
  bitmap bitmap = bitmapdrawable.getbitmap();
  bitmapdrawable = new bitmapdrawable(toroundcorner(bitmap, pixels));
  return bitmapdrawable;
 }

 /**
  * 读取路径中的图片,然后将其转化为缩放后的bitmap
  *
  * @param path
  */
 public static void savebefore(string path) {
  bitmapfactory.options options = new bitmapfactory.options();
  options.injustdecodebounds = true;
  // 获取这个图片的宽和高
  bitmap bitmap = bitmapfactory.decodefile(path, options); // 此时返回bm为空
  options.injustdecodebounds = false;
  // 计算缩放比
  int be = (int) (options.outheight / (float) 200);
  if (be <= 0)
   be = 1;
  options.insamplesize = 2; // 图片长宽各缩小二分之一
  // 重新读入图片,注意这次要把options.injustdecodebounds 设为 false哦
  bitmap = bitmapfactory.decodefile(path, options);
  int w = bitmap.getwidth();
  int h = bitmap.getheight();
  system.out.println(w + "   " + h);
  // savepng_after(bitmap,path);
  savejpge_after(bitmap, path);
 }

 /**
  * 保存图片为png
  *
  * @param bitmap
  * @param name
  */
 public static void savepng_after(bitmap bitmap, string name) {
  file file = new file(name);
  try {
   fileoutputstream out = new fileoutputstream(file);
   if (bitmap.compress(bitmap.compressformat.png, 100, out)) {
    out.flush();
    out.close();
   }
  } catch (filenotfoundexception e) {
   e.printstacktrace();
  } catch (ioexception e) {
   e.printstacktrace();
  }
 }

 /**
  * 保存图片为jpeg
  *
  * @param bitmap
  * @param path
  */
 public static void savejpge_after(bitmap bitmap, string path) {
  file file = new file(path);
  try {
   fileoutputstream out = new fileoutputstream(file);
   if (bitmap.compress(bitmap.compressformat.jpeg, 100, out)) {
    out.flush();
    out.close();
   }
  } catch (filenotfoundexception e) {
   e.printstacktrace();
  } catch (ioexception e) {
   e.printstacktrace();
  }
 }

 /**
  * 图片合成
  *
  * @param bitmap
  * @return
  */
 private bitmap createbitmap(bitmap src, bitmap watermark) {
  if (src == null) {
   return null;
  }
  int w = src.getwidth();
  int h = src.getheight();
  int ww = watermark.getwidth();
  int wh = watermark.getheight();
  // create the new blank bitmap
  bitmap newb = bitmap.createbitmap(w, h, config.argb_8888);// 创建一个新的和src长度宽度一样的位图
  canvas cv = new canvas(newb);
  // draw src into
  cv.drawbitmap(src, 0, 0, null);// 在 0,0坐标开始画入src
  // draw watermark into
  cv.drawbitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印
  // save all clip
  cv.save(canvas.all_save_flag);// 保存
  // store
  cv.restore();// 存储
  return newb;
 }

 // 将图片转换成byte[]以便能将其存到数据库
 public static byte[] getbytefrombitmap(bitmap bitmap) {
  bytearrayoutputstream out = new bytearrayoutputstream();
  bitmap.compress(bitmap.compressformat.jpeg, 100, out);
  try {
   out.flush();
   out.close();
  } catch (ioexception e) {
   e.printstacktrace();
   // log.e(tag, "transform byte exception");
  }
  return out.tobytearray();
 }

 // 得到存储在数据库中的图片
 // eg imageview.setimagebitmap(bitmapobj);
 public static bitmap getbitmapfrombyte(byte[] temp) {
  if (temp != null) {
   bitmap bitmap = bitmapfactory.decodebytearray(temp, 0, temp.length);
   return bitmap;
  } else {
   // bitmap bitmap=bitmapfactory.decoderesource(getresources(),
   // r.drawable.contact_add_icon);
   return null;
  }
 }
    //将手机中的文件转换为bitmap类型
 public static bitmap getbitemapfromfile(file f) {
  if (!f.exists())
   return null;
  try {
   return bitmapfactory.decodefile(f.getabsolutepath());
  } catch (exception ex) {
   return null;
  }
 }
 //将手机中的文件转换为bitmap类型(重载+1)
 public static bitmap getbitemapfromfile(string filename) {

  try {
   return bitmapfactory.decodefile(filename);
  } catch (exception ex) {
   return null;
  }
 }

}