This article summarizes the picture-related code for Android programming. Share it for your reference, as follows:
1. Convert Bitmap into string:
/** * @param bitmap * @return Converted string */ public static String bitmapToString(Bitmap bitmap) { // Convert Bitmap to string String string = null; ByteArrayOutputStream bStream = new ByteArrayOutputStream(); (, 100, bStream); byte[] bytes = (); string = (bytes, ); return string; }
2. Convert string to Bitmap:
/** * @param string * @return The bitmap converted into */ public static Bitmap stringToBitmap(String string) { // Convert string to Bitmap type Bitmap bitmap = null; try { byte[] bitmapArray; bitmapArray = (string, ); bitmap = (bitmapArray, 0, ); } catch (Exception e) { (); } return bitmap; }
Convert to Drawable:
/** * @param bitmap Bitmap image * @return Drawable The converted Drawable object */ public static Drawable bitmapToDrawable(Bitmap bitmap) { if (bitmap == null) return null; if (160 != ()) { (160); } return new BitmapDrawable(bitmap); }
Get the Drawable object according to the image resource ID:
/** * @param context context * @param id Image resource ID * @return Drawable object */ public static Drawable resourceToDrawable(Context context,int id) { return null == context ? null : bitmapToDrawable(((), id)); }
byte array converts Drawble objects:
/** * @param bytes byte array * @return drawble object */ public static Drawable byteArrayToDrawable(byte[] bytes) { return null == bytes ? null : bitmapToDrawable((bytes, 0, )); }
Convert 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(); }
Array conversion Bitmap object:
/** * @param bytes byte array * @return bitmap object */ public static Bitmap byteArrayToBitmap(byte[] bytes) { return null == bytes ? null : (bytes, 0, ); }
6. The picture is colored and returns to grayscale pictures (old-style pictures):
/** * @param bitmap The bitmap passed in * @return Image Bitmap object after color removal */ public static Bitmap toGrayscale(Bitmap bitmap) { int width,height; height = (); width = (); Bitmap bmpGrayscale = (width, height, .RGB_565); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); (0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); (f); (bitmap, 0, 0, paint); return bmpGrayscale; }
7. Scale the picture:
/** * @param url The path to the picture * @param requireSize Scaled size * @return Scaled image Bitmap object */ public static Bitmap getScaleImage(String url,int requireSize) { o = new (); // This property means that the image is not loaded into memory, but only the attributes of the image are read, including the height and width of the image = true; (url, o); int width_tmp = ,height_tmp = ; int scale = 1; while (true) { if (width_tmp / 2 < requireSize || height_tmp / 2 < requireSize) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } o2 = new (); = scale; Bitmap bmp = (url, o2); return bmp; }
8. Obtain the reflection of the picture and reflect the gradient effect at the same time:
/** * @param bitmap Image source * @return Processed image Bitmap object */ public static Bitmap createMirro(Bitmap bitmap) { int width = (); int height = (); int shadow_height = 15; int[] pixels = new int[width * height]; (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] >> 16) & 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 = (width, height, .ARGB_8888); for (int y = 0; y < height; y++) { (pixels, y * width, width, 0, height - y - 1, width, 1); } return (bm, 0, 0, width, shadow_height); }
9. Save the picture to SDCard:
/** * @param imagePath image saving 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 (()) { return; } else { try { File parentFile = (); if (!()) { (); } (); FileOutputStream fos; fos = new FileOutputStream(f); (, 100, fos); (); } catch (FileNotFoundException e) { (); (); } catch (IOException e) { (); (); } } }
10. Get the picture from SDCard:
/** * @param imagePath The path to save the image in SDCard * @return Return the saved bitmap object */ public static Bitmap getImageFromLocal(String imagePath) { File file = new File(imagePath); if (()) { Bitmap bitmap = (imagePath); (()); return bitmap; } return null; }
11. Image compression processing:
/** * The image is compressed mainly to solve the OOM problem caused by excessive image display, which is displayed by the control, which consumes memory. * Generally, the compressed image size should be similar to the control used to display it. * @param context context * @param resId Image Resource Id * @param reqWidth expects compression width * @param reqHeight The height of compression expected * @return Compressed picture */ public static Bitmap compressBitmapFromResourse(Context context, int resId, int reqWidth, int reqHeight) { final options = new (); /* * When parsing for the first time, inJustDecodeBounds is set to true, * It is prohibited to allocate memory for bitmap. Although the return value of bitmap is empty, the image size can be obtained. */ = true; ((), resId, options); final int height = ; final int width = ; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = ((float) height / (float) reqHeight); final int widthRatio = ((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } = inSampleSize; // Use the calculated inSampleSize value to parse the picture again = false; return ((), resId, options); }
12. Get the maximum value of available memory (App uses memory that exceeds this value, causing OutOfMemory exception):
private int getMaxMemoryForApp() { int maxMemory = (int) (().maxMemory() / 1024); return maxMemory; }
13. Crop the picture into circles:
/** * Process Bitmap into a circle image * @param bitmap processed before bitmap * @return The bitmap after processing */ public static Bitmap circlePic(Bitmap bitmap){ int width = (); int height = (); int r = width < height ? width/2:height/2;//The radius of the circle is smaller in width and high school, so that there is no blank space Bitmap outBitmap = (r*2, r*2, .ARGB_8888);//Create a Bitmap that is exactly 2r size Canvas canvas = new Canvas(outBitmap); final int color =0xff424242; final Paint paint = new Paint(); /** * A square in the center of the image to be used to clip in the original image * The coordinates are as follows: * 1. If w < h , upper left coordinate (0, (h-w)/2) and upper right coordinate (w, (h+w)/2) are offset by 10 * 2. If w > h , the upper left coordinate ((w-h)/2, 0) , the upper right coordinate ((w+h)/2, h) is offset by 10 */ 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 to set the display of canvas and set the canvas clipping final Rect rect2 = new Rect( 0, 0, r*2, r*2); //Improve accuracy, used to eliminate aliasing final RectF rectF = new RectF(rect2); //The following is the setting of the brush and canvas (true); (0,0,0,0); (color); //Set rounded corners, the radius is r, and the size is rect2 (rectF, r, r, paint); //Set the display method when images overlap (new PorterDuffXfermode(.SRC_IN)); //Draw the image to canvas (bitmap, rect, rect2, paint); return outBitmap; } }
I hope this article will be helpful to everyone's Android programming design.