Android獲取當前手機設備信息工具類

f663x 9年前發布 | 5K 次閱讀 Java Android

工作中用到的圖片處理工具類,簡單寫下來,以便備用!

public class BitmapUtils {

/**
 * 圖像背景圓角處理
 * bitmap要處理的圖片 roundPx 圖片彎角的圓度一般是5到10之間
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    // 創建與原圖大小一樣的bitmap文件,Config.ARGB_8888根據情況可以改用其它的
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    // 實例畫布,繪制的bitmap將保存至output中
    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);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    bitmap.recycle();
    bitmap = null;
    return output;
}

/**
 * bitmap縮放
 * width要縮放的寬度 height要縮放的高度
 */
public static Bitmap getBitmapDeflation(Bitmap bitmap, int width, int height, boolean recycle) {

    if (null == bitmap) {

      return null;

      }
    float scaleWidth = 0f;
    float scaleHeight = 0f;
    // 獲取bitmap寬高
    int bitmapWidth = bitmap.getWidth();
    int bitmapHeight = bitmap.getHeight();
    // 計算縮放比,圖片的寬高小于指定的寬高則不縮放
    if (width < bitmapWidth) {
        scaleWidth = ((float) width) / bitmapWidth;
    } else {
        scaleWidth = 1.00f;
    }
    if (height < bitmapHeight) {
        scaleHeight = ((float) height) / bitmapHeight;
    } else {
        scaleHeight = 1.00f;
    }
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
    if (recycle && !bitmap.isRecycled()) {
        bitmap.recycle();
    }
    bitmap = null;

    return newBitmap;
}

/**
 *
 * 方法概述:進入圖片的大小與質量壓縮,用于區分大小圖片
 */
public static Bitmap getCompressedImage(String srcPath) {
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    // 開始讀入圖片,此時把options.inJustDecodeBounds 設回true了
    newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
    newOpts.inPurgeable = true;
    newOpts.inJustDecodeBounds = true;
    FileInputStream is = null;
    try {
        is = new FileInputStream(srcPath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(is, null, newOpts);// 此時返回bm為空
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    // 現在主流手機比較多是800*480分辨率,所以高和寬我們設置為
    float hh = 130f;// 這里設置高度為800f
    float ww = 130f;// 這里設置寬度為480f
    // 縮放比。由于是固定比例縮放,只用高或者寬其中一個數據進行計算即可
    int be = 1;// be=1表示不縮放
    if (w > h && w > ww) {// 如果寬度大的話根據寬度固定大小縮放
        be = (int) (newOpts.outWidth / ww);
    } else if (w < h && h > hh) {// 如果高度高的話根據寬度固定大小縮放
        be = (int) (newOpts.outHeight / hh);
    }
    if (be <= 0) {
        be = 1;
    }
    newOpts.inSampleSize = be;// 設置縮放比例
    // 重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了
    bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
    if (bitmap != null) { return compressImageSize(bitmap, 8);// 壓縮好比例大小后再進行質量壓縮
    }
    return null;
}

/**
 *
 * 方法概述:圖片質量壓縮
 */
protected static Bitmap compressImageSize(Bitmap image, int size) {
    if (image == null)
        return image;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 質量壓縮方法,這里10表示不壓縮,把壓縮后的數據存放到baos中
    int options = 100;
    while (baos.toByteArray().length / 1000 > size && options / 3 > 0) { // 循環判斷如果壓縮后圖片是否大于10kb,大于繼續壓縮
        baos.reset();// 重置baos即清空baos
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這里壓縮options%,把壓縮后的數據存放到baos中
        options -= options / 3;// 每次都減少30%
    }

    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮后的數據baos存放到ByteArrayInputStream中
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數據生成圖片
    return bitmap;
}

/**
 *
 * 方法概述:保存圖片
 */
public static String saveBitmapWithName(String path, String bitName, Bitmap mBitmap) {
    File f = new File(path + bitName + ".png");
    String url = path + bitName + ".png";
    try {
        f.createNewFile();
    } catch (IOException e) {
        System.out.println("文件創建出錯");
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        System.out.println(" 創建文件流失敗");
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return url;
}

/**
 *
 * 方法概述:根據傳入參數保存圖片
 */
public static boolean saveImageTo(Bitmap photo, String spath) {
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(spath, false));
        photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
        bos.flush();
        bos.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

} </pre>

 本文由用戶 f663x 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!