图片处理工具类
来源:互联网 发布:手机二维码扫描软件 编辑:程序博客网 时间:2024/05/21 09:39
/** * 根据原图和变长绘制圆形图片 * * @param source * @param min * @return */ public static Bitmap createCircleImage(Bitmap source, int min) { final Paint paint = new Paint(); paint.setAntiAlias(true); Bitmap target = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888); /** * 产生一个同样大小的画布 */ Canvas canvas = new Canvas(target); /** * 首先绘制圆形 */ canvas.drawCircle(min / 2, min / 2, min / 2, paint); /** * 使用SRC_IN */ paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); /** * 绘制图片 */ canvas.drawBitmap(source, 0, 0, paint); return target; }
/** * 得到有一定透明度的图片 * * @param sourceImg * @param number * @return */ public static Bitmap getTransparentBitmap(Bitmap sourceImg, int number) { if (sourceImg == null) { return null; } int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()]; sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg .getWidth(), sourceImg.getHeight());// 获得图片的ARGB值 number = number * 255 / 100; for (int i = 0; i < argb.length; i++) { argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF); } sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg .getHeight(), Bitmap.Config.ARGB_8888); return sourceImg; }
/** * 保存bitmap文件到指定路径 * * @param mBitmap * @param path * @return */ public static boolean saveMyBitmap(Bitmap mBitmap, String path) { if (mBitmap == null) { return false; } File f = new File(path); FileOutputStream fOut = null; Log.e("saveMyBitmap", "File Path:" + f.getAbsolutePath()); try { fOut = new FileOutputStream(f); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); fOut.flush(); fOut.close(); return true; } catch (IOException e) { e.printStackTrace(); } return false; }
package com.qhcloud.home.utils;import android.app.Activity;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.PixelFormat;import android.graphics.Rect;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.os.AsyncTask;import android.support.annotation.DrawableRes;import android.widget.ImageView;import com.google.zxing.BarcodeFormat;import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.QRCodeWriter;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.lang.ref.WeakReference;import java.net.HttpURLConnection;import java.net.URL;/** * BitmapUtil工具类 */public class BitmapUtil { /** * 生成bitmap文件 * * @param context * @param resId * @return */ public static Bitmap getBitmap(Context context, int resId) { Resources res = context.getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, resId); return bmp; } /** * 计算出原始图片的长度和宽度 * * @param options * @param minSideLength * @param maxNumOfPixels * @return */ private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math .sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } } /** * bitmap转字节数组 * * @param bm * @return */ public static byte[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } /** * 字节数组转bitmap * * @param b * @return */ public static Bitmap Bytes2Bimap(byte[] b) { if (b.length != 0) { return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } } /** * 从文件路径中获取bitmap * * @param path * @return */ public static Bitmap getBitmapFromFile(String path) { Bitmap bitmap = null; try { FileInputStream fis = new FileInputStream(path); bitmap = BitmapFactory.decodeStream(fis); } catch (FileNotFoundException e) { e.printStackTrace(); } return bitmap; } /** * drawable转bitmap * * @param drawable * @return */ public static Bitmap drawableToBitmap(Drawable drawable) { // 取 drawable 的长宽 int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); // 取 drawable 的颜色格式 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 建立对应 bitmap Bitmap bitmap = Bitmap.createBitmap(w, h, config); // 建立对应 bitmap 的画布 Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); // 把 drawable 内容画到画布中 drawable.draw(canvas); return bitmap; } /** * drawable缩放 * * @param drawable * @param w * @param h * @return */ public static Drawable zoomDrawable(Drawable drawable, int w, int h) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); // drawable转换成bitmap Bitmap oldbmp = drawableToBitmap(drawable); // 创建操作图片用的Matrix对象 Matrix matrix = new Matrix(); // 计算缩放比例 float sx = ((float) w / width); float sy = ((float) h / height); // 设置缩放比例 matrix.postScale(sx, sy); // 建立新的bitmap,其内容是对原bitmap的缩放后的图 Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); return new BitmapDrawable(newbmp); } /** * 可用于生成缩略图 * Creates a centered bitmap of the desired size. Recycles the input. * * @param source */ public static Bitmap extractMiniThumb(Bitmap source, int width, int height) { return extractMiniThumb(source, width, height, true); } /** * 保存bitmap到指定文件路径 * * @param bitmap * @param path * @return */ public static boolean saveBitmap(Bitmap bitmap, String path) { try { File file = new File(path); if (file.exists()) { file.delete(); } FileOutputStream out = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); out.flush(); out.close(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; } /** * 生成二维码 * * @param activity * @param content * @return */ public static Bitmap generateQRCode(Activity activity, String content) { try { int width = AndroidUtil.getDisplayMetrics(activity).widthPixels * 4 / 5; QRCodeWriter writer = new QRCodeWriter(); // MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, width, width); return bitMatrix2Bitmap(matrix); } catch (WriterException e) { e.printStackTrace(); } return null; } /** * 位阵bitMatrix转位图 * * @param matrix * @return */ private static Bitmap bitMatrix2Bitmap(BitMatrix matrix) { int w = matrix.getWidth(); int h = matrix.getHeight(); int[] rawData = new int[w * h]; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int color = Color.WHITE; if (matrix.get(i, j)) { color = Color.BLACK; } rawData[i + (j * w)] = color; } } Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565); bitmap.setPixels(rawData, 0, w, 0, 0, w, h); return bitmap; } /** * 得到新尺寸的bitmap * * @param bm * @param newHeight * @param newWidth * @return */ public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); } /** * 从资源文件中生成bitmap * * @param mContext * @param resId * @param reqWidth * @param reqHeight * @return */ public static Bitmap decodeBitmapFromResource(Context mContext, @DrawableRes int resId, int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(mContext.getResources(), resId, options); options.inSampleSize = calculateInSampleSize(mContext, resId, reqWidth, reqHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(mContext.getResources(), resId, options); } /** * 获取网络图片资源 * * @param url * @return */ public static Bitmap getHttpBitmap(String url) { URL myFileURL; Bitmap bitmap = null; try { myFileURL = new URL(url); //获得连接 HttpURLConnection conn = (HttpURLConnection) myFileURL.openConnection(); //设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制 conn.setConnectTimeout(6000); //连接设置获得数据流 conn.setDoInput(true); //不使用缓存 conn.setUseCaches(false); //这句可有可无,没有影响 //conn.connect(); //得到数据流 InputStream is = conn.getInputStream(); //解析得到图片 bitmap = BitmapFactory.decodeStream(is); //关闭数据流 is.close(); } catch (Exception e) { e.printStackTrace(); } return bitmap; }}
0 0
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 图片处理工具类
- 处理图片的工具类
- Android图片处理工具类
- android 图片处理工具类
- Bitmap 图片处理工具类
- android 处理图片工具类
- Android图片处理工具类
- java图片处理工具类
- Java图片处理工具类
- 处理图片的工具类
- 处理图片的工具类
- Sqlite 存储自定义对象
- cocos2dx3.0 打飞机完整项目
- 指针的用法
- HBase集群搭建
- 【LeetCode】 007. Reverse Integer
- 图片处理工具类
- 一个利用Bootstrap写的的小作品
- 原生分页后搜索
- DP+KMP——Another Meaning ( HDU 5763 ) ( 2016 Multi-University Training Contest 4 1001 )
- iOS _Quartz2D---线段绘制方式(代码)
- Codeforces 104A Blackjack
- 第一次的软件经验
- android sensors
- thinkphp5 tablib标签库自定义