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;
/**
* Tool class for processing pictures.
*
*/
public class Imagetools {
/** */
/**
* Picture go color, return grayscale picture
*
* @param bmporiginal
* Incoming Pictures
* @return Color-After image
*/
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;
}
/** */
/**
* Go to color and add rounded corners
*
* @param bmporiginal
* Original Artwork
* @param pixels
* Fillet Radian
* @return Modified Picture
*/
public static Bitmap Tograyscale (Bitmap bmporiginal, int pixels) {
Return Toroundcorner (Tograyscale (bmporiginal), pixels);
}
/** */
/**
* Turn the picture into rounded corners
*
* @param bitmap
* Need to modify the picture
* @param pixels
* The radian of rounded corners
* @return Fillet picture
*/
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;
}
/** */
/**
* Enable fillet function to support 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;
}
/**
* Read the picture in the path and convert it to the scaled bitmap
*
* @param path
*/
public static void Savebefore (String path) {
Bitmapfactory.options Options = new Bitmapfactory.options ();
Options.injustdecodebounds = true;
Gets the width and height of this picture
Bitmap Bitmap = bitmapfactory.decodefile (path, options); Return BM to NULL at this time
Options.injustdecodebounds = false;
Calculate scaling ratio
int be = (int) (Options.outheight/(float) 200);
if (be <= 0)
be = 1;
Options.insamplesize = 2; Picture long width each reduced one-second
Re-read the picture, note this time to set the Options.injustdecodebounds to False OH
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);
}
/**
* Save Picture as 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, out)) {
Out.flush ();
Out.close ();
}
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Save Picture as 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, out)) {
Out.flush ();
Out.close ();
}
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Picture synthesis
*
* @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);//Create a new bitmap with the same width as src length
Canvas CV = new Canvas (NEWB);
Draw Src into
Cv.drawbitmap (SRC, 0, 0, NULL);//at 0, 0 coordinates begin to draw into SRC
Draw Watermark into
Cv.drawbitmap (watermark, W-WW + 5, H-WH + 5, NULL);//watermark in the lower right corner of SRC
Save All Clip
Cv.save (Canvas.all_save_flag);//Save
Store
Cv.restore ()//Storage
return newb;
}
Convert a picture to byte[] so you can save it to a database
public static byte[] Getbytefrombitmap (Bitmap Bitmap) {
Bytearrayoutputstream out = new Bytearrayoutputstream ();
Bitmap.compress (Bitmap.CompressFormat.JPEG, out);
try {
Out.flush ();
Out.close ();
catch (IOException e) {
E.printstacktrace ();
LOG.E (TAG, "transform byte exception");
}
return Out.tobytearray ();
}
Get the picture stored in the database
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;
}
}
Convert a file from a phone to a bitmap type
public static Bitmap Getbitemapfromfile (File f) {
if (!f.exists ())
return null;
try {
Return Bitmapfactory.decodefile (F.getabsolutepath ());
catch (Exception ex) {
return null;
}
}
Converts a file in the phone to the bitmap type (overload + 1)
public static Bitmap Getbitemapfromfile (String fileName) {
try {
Return Bitmapfactory.decodefile (FileName);
catch (Exception ex) {
return null;
}
}
}