An example of this article summarizes the Android programming picture-related code. Share to everyone for your reference, specific as follows:
1. Convert Bitmap to String:
/**
* @param bitmap
* @return converted to a string * * Public
static string bitmaptostring (Bitmap Bitmap) {
// Converts a bitmap to a strings string
= null;
Bytearrayoutputstream Bstream = new Bytearrayoutputstream ();
Bitmap.compress (Compressformat.png, bstream);
byte[] bytes = Bstream.tobytearray ();
String = base64.encodetostring (bytes, base64.default);
return string;
}
2. String conversion to bitmap:
/**
* @param string String
* @return converted to bitmap
/public
static Bitmap Stringtobitmap (String string) {
//Will The string is converted to Bitmap type
Bitmap Bitmap = null;
try {
byte[] bitmaparray;
Bitmaparray = Base64.decode (string, base64.default);
Bitmap = Bitmapfactory.decodebytearray (Bitmaparray, 0, bitmaparray.length);
} catch (Exception e) {
e.printstacktrace ();
}
return bitmap;
}
3.Bitmap Conversion to drawable:
/**
* @param bitmap bitmap bitmap Image
* @return drawable converted Drawable object/public
static drawable Bitmaptodrawable (Bitmap Bitmap) {
if (Bitmap = = null) return
null;
if (160!= bitmap.getdensity ()) {
bitmap.setdensity (160);
}
return new bitmapdrawable (bitmap);
}
Get the Drawable object based on the picture resource ID:
/**
* @param context
* @param ID picture's resource ID
* @return drawable object/public
static drawable Resourcetodrawable (context Context,int ID) {return
NULL = context? Null:bitmaptodrawable ( Bitmapfactory.decoderesource (Context.getresources (), id);
}
byte array conversion drawble object:
/**
* @param bytes byte array
* @return drawble object
/public static drawable bytearraytodrawable (byte[) bytes) {return
null = bytes? null:bitmaptodrawable (Bitmapfactory.decodebytearray (bytes, 0, bytes.length));
}
4.Drawable Conversion to bitmap:
/**
* Drawble object to Bitmap object
* @param drawable drawble Object
* @return Bitmap object/public
static Bitmap Drawabletobitmap (drawable drawable) {return
null = drawable? Null: ((bitmapdrawable) drawable). Getbitmap ( );
}
5.byte Array Conversion Bitmap object:
/**
* @param bytes byte array
* @return Bitmap object
/public static Bitmap bytearraytobitmap (byte[) bytes) { C5/>return null = = bytes? Null:BitmapFactory.decodeByteArray (bytes, 0, bytes.length);
6. Picture go color, return grayscale picture (old picture):
/**
* @param bitmap incoming bitmap
* @return color Image Bitmap Object
/public
static bitmap Tograyscale (bitmap Bitmap) {
int width,height;
Height = bitmap.getheight ();
width = Bitmap.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 (bitmap, 0, 0, paint);
return bmpgrayscale;
}
7. Zoom to the Picture:
/**
* @param url picture Path
* @param requiresize scaled size
* @return scaled picture Bitmap object
/public static Bitmap getscaleimage (String url,int requiresize) {
bitmapfactory.options o = new Bitmapfactory.options ();
This property indicates that the picture is not loaded into memory, but simply reads the properties of the picture, including the picture's high width
o.injustdecodebounds = true;
Bitmapfactory.decodefile (URL, o);
int width_tmp = O.outwidth,height_tmp = O.outheight;
int scale = 1;
while (true) {
if (Width_tmp/2 < requiresize | | | HEIGHT_TMP/2 < requiresize) break
;
Width_tmp/= 2;
Height_tmp/= 2;
Scale *= 2;
}
Bitmapfactory.options O2 = new Bitmapfactory.options ();
O2.insamplesize = scale;
Bitmap bmp = Bitmapfactory.decodefile (URL, O2);
return bmp;
}
8. Get the image of the reflection, while the reflection of the gradient effect:
/** * @param bitmap Picture Source * @return-processed Picture Bitmap object/public static bitmap Createmirro (bitmap bitmap) {int width =
Bitmap.getwidth ();
int height = bitmap.getheight ();
int shadow_height = 15;
int[] pixels = new int[width * height];
Bitmap.getpixels (pixels, 0, width, 0, 0, width, height);
Shadow effect int alpha = 0x00000000;
for (int y = 0; y < height; y++) {for (int x = 0; x < width; x +) {int index = y * width + x;
int r = (Pixels[index] >>) & 0xFF;
int g = (Pixels[index] >> 8) & 0xFF;
int b = Pixels[index] & 0xff; Pixels[index] = Alpha | (r << 16) | (g << 8) |
b
} if (y >= (height-shadow_height)) {alpha = alpha + 0x1f000000;
}///Invert effect Bitmap BM = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
for (int y = 0; y < height; y++) {bm.setpixels (pixels, y * width, width, 0, height-y-1, width, 1); } return BiTmap.createbitmap (BM, 0, 0, width, shadow_height);
}
9. Save picture to SDcard:
/**
* @param imagepath Picture save path
* @param BM Saved Bitmap Object *
/public
static void Saveimgtolocal (String ImagePath, Bitmap BM) {
if (BM = NULL | | imagepath = NULL | | "". Equals (ImagePath)) {return
;
}
File F = new file (ImagePath);
if (f.exists ()) {return
;
} else {
try {
File parentfile = F.getparentfile ();
if (!parentfile.exists ()) {
parentfile.mkdirs ();
}
F.createnewfile ();
FileOutputStream Fos;
FOS = new FileOutputStream (f);
Bm.compress (Bitmap.CompressFormat.PNG, FOS);
Fos.close ();
} catch (FileNotFoundException e) {
f.delete ();
E.printstacktrace ();
} catch (IOException e) {
e.printstacktrace ();
F.delete ();}}
10. Get pictures from SDcard:
/**
* @param imagepath picture saved in sdcard
* @return Returns the saved Bitmap object/public
static Bitmap Getimagefromlocal (String imagepath) {
File File = new file (ImagePath);
if (file.exists ()) {
Bitmap Bitmap = Bitmapfactory.decodefile (ImagePath);
File.setlastmodified (System.currenttimemillis ());
return bitmap;
}
return null;
}
11. Image compression Processing:
/** * The image is compressed, mainly to solve the control display too large picture memory caused by oom problems.
* The general compressed picture size should be similar to the size of the control used to display it.
* @param context * @param resid picture Resource ID * @param reqwidth expected compression width * @param reqheight expected compression height * @return Compressed picture * * public static Bitmap Compressbitmapfromresourse (context context, int resid, int reqwidth, int reqheight) {final Bitmap
Factory.options Options = new Bitmapfactory.options (); * * The first resolution, Injustdecodebounds set to True, * prohibit allocating memory for bitmap, although bitmap return value is null, but can get picture size * * Options.injustdecodebound
s = true;
Bitmapfactory.decoderesource (Context.getresources (), resid, options);
Final int height = options.outheight;
Final int width = options.outwidth;
int insamplesize = 1; if (Height > reqheight | | | width > reqwidth) {final int heightratio = Math.Round (float) height/(float) Reqhei
Ght);
Final int widthRatio = Math.Round ((float) width/(float) reqwidth); Insamplesize = HeightRatio < WidthRatio?
Heightratio:widthratio; } options.insamplesize = InsampLesize;
The computed insamplesize value is used again to parse the picture options.injustdecodebounds = false;
Return Bitmapfactory.decoderesource (Context.getresources (), resid, options);
}
12. Get maximum available memory (app using memory exceeds this value to cause OutOfMemory exception):
private int Getmaxmemoryforapp () {
int maxmemory = (int) (Runtime.getruntime (). MaxMemory ()/1024);
return maxmemory;
}
13. Crop the picture into a circle:
/** * Bitmap processing as a circular picture * @param bitmap the bitmap before processing * * @return processed bitmap/public static bitmap Circlepic (bitmap bitmap) {
int width = bitmap.getwidth ();
int height = bitmap.getheight (); int r = Width < height? width/2:height/2;//Circle radius, width and high school smaller, so as to show no blank Bitmap outbitmap = Bitmap.createbitmap (r*2, r*2, Bitmap.Config.ARGB_8888)
;//Create a bitmap Canvas that is just 2r in size Canvas = new Canvas (OUTBITMAP);
final int color =0xff424242;
Final Paint Paint = new Paint (); /** * A square that intercepts the center of the image, used in the original artwork to intercept the coordinates as follows: * 1. If w < h, upper left coordinate (0, (h-w)/2), the upper right coordinate (w, (H+W)/2) is offset 10 * 2. If W & Gt H, upper left coordinate ((w-h)/2, 0), upper right coordinate ((W+H)/2, H) offset/final Rect Rect = new Rect (Width < height 0: (width-height)/ 2, Width < height? (height-width)/2-10: -10, width < height? Width: (width+height)/2, (Width < height?)
(height+width)/2-10:height-10));
Create a square of diameter size to set the canvas display and set the canvas to intercept the final Rect rect2 = new Rect (0, 0, r*2, r*2); Improved accuracy for anti-aliasing final ReCtF RECTF = new RECTF (RECT2);
The following is the set brush and canvas Paint.setantialias (true);
Canvas.drawargb (0,0,0,0);
Paint.setcolor (color);
Set rounded corners, radius r, size Rect2 canvas.drawroundrect (RECTF, R, R, paint);
Sets how the image is displayed when it overlaps Paint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.SRC_IN));
Draw images to canvas canvas.drawbitmap (bitmap, rect, rect2, paint);
return outbitmap;
}
}
I hope this article will help you with your Android programming.