- public class BitmapHelper {
- //图片转byte
- public static byte[] Bitmap2BytesJpeg(Bitmap bitmap) {
- byte[] result = null;
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- if (bitmap != null) {
- try {
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
- result = bos.toByteArray();
- bos.close();
- } catch (OutOfMemoryError ex) {
- ex.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return result;
- }
- public static byte[] Bitmap2BytesPng(Bitmap bitmap) {
- byte[] result = null;
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- if (bitmap != null) {
- try {
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
- result = bos.toByteArray();
- bos.close();
- } catch (OutOfMemoryError ex) {
- ex.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return result;
- }
- public static Bitmap Bytes2Bitmap(byte[] data) {
- Bitmap bitmap = null;
- if (data != null) {
- try {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inPreferredConfig = Bitmap.Config.RGB_565;
- bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
- options);
- } catch (OutOfMemoryError e) {
- e.printStackTrace();
- }
- }
- return bitmap;
- }
- //按比例生成图片
- public static Bitmap bitmapToFixedBitmap(Bitmap bitmap, int width,
- int height) {
- Bitmap result = null;
- if (bitmap != null) {
- int x = bitmap.getWidth();
- int y = bitmap.getHeight();
- float f1 = width / x;
- float f2 = height / y;
- Matrix matrix = new Matrix();
- matrix.postScale(f1, f2);
- Bitmap.createBitmap(bitmap, 0, 0, x, y, matrix, true);
- }
- return result;
- }
- //位图缩放
- public static Bitmap bitmapToScaleBitmap(Bitmap bitmap, int width,
- int height) {
- Bitmap result = null;
- if (bitmap != null) {
- int x = bitmap.getWidth();
- int y = bitmap.getHeight();
- float f1 = width / Math.max(x, y);
- float f2 = height / Math.max(x, y);
- Matrix matrix = new Matrix();
- matrix.postScale(f1, f2);
- Bitmap.createBitmap(bitmap, 0, 0, x, y, matrix, true);
- }
- return result;
- }
- public static Bitmap drawableToBitmap(Drawable drawable) {
- Bitmap bitmap = null;
- int width = drawable.getIntrinsicWidth();
- int height = drawable.getIntrinsicHeight();
- if (drawable.getOpacity() != -1) {
- bitmap = Bitmap
- .createBitmap(
- width,
- height,
- drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
- : Bitmap.Config.RGB_565);
- Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, width, height);
- drawable.draw(canvas);
- }
- return bitmap;
- }
- public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
- return getRoundCornerBitmap(bitmap, 6.0F);
- }
- /**
- * 生成圆角图片
- * @param bitmap
- * @param radius
- */
- public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float radius) {
- Bitmap result = null;
- if (bitmap != null) {
- result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
- Bitmap.Config.ARGB_8888);
- Canvas canvas = new Canvas(result);
- Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- paint.setColor(-12434878);
- Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
- RectF rectF = new RectF(rect);
- canvas.drawRoundRect(rectF, radius, radius, paint);
- paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
- canvas.drawBitmap(bitmap, rect, rect, paint);
- }
- return result;
- }
- /**
- * 生成带倒影图片
- */
- public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
- final int reflectionGap = 4;
- int width = bitmap.getWidth();
- int height = bitmap.getHeight();
- Matrix matrix = new Matrix();
- matrix.preScale(1, -1);
- Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
- width, height / 2, matrix, false);
- Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
- (height + height / 2), Bitmap.Config.ARGB_8888);
- ;
- Canvas canvas = new Canvas(bitmapWithReflection);
- canvas.drawBitmap(bitmap, 0, 0, null);
- Paint paint = new Paint();
- canvas.drawRect(0, height, width, height + reflectionGap, paint);
- canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
- Paint p = new Paint();
- LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
- bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
- 0x00ffffff, TileMode.CLAMP);
- p.setShader(shader);
- p.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
- canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
- + reflectionGap, p);
- return bitmapWithReflection;
- }
- }