茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    Android圖片壓縮
    來源:易賢網(wǎng) 閱讀:1046 次 日期:2015-04-09 17:25:17
    溫馨提示:易賢網(wǎng)小編為您整理了“Android圖片壓縮”,方便廣大網(wǎng)友查閱!

    1、質(zhì)量壓縮方法

    通過compress的方法只是減小了文件的大小,但是并不能保證減低bitmap文件解碼后在內(nèi)存的占用量。

    private Bitmap compressImage(Bitmap image) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    //質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中

    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);

    int options = 100;

    while ( baos.toByteArray().length / 1024>100) { //循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮

    baos.reset();//重置baos即清空baos

    //這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中

    image.compress(Bitmap.CompressFormat.JPEG, options, baos);

    options -= 10;//每次都減少10

    }

    //把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中

    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());

    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數(shù)據(jù)生成圖片

    return bitmap;

    }

    2、按比例大小壓縮方法

    對Bitmap形式的圖片進(jìn)行壓縮, 也就是通過設(shè)置采樣率, 減少Bitmap的像素, 從而減少了它所占用的內(nèi)存

    ① 根據(jù)路徑獲取圖片并壓縮

    private Bitmap getimage(String srcPath) {

    BitmapFactory.Options newOpts = new BitmapFactory.Options();

    //開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了

    newOpts.inJustDecodeBounds = true;//只讀邊,不讀內(nèi)容

    Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此時(shí)返回bm為空

    newOpts.inJustDecodeBounds = false;

    int w = newOpts.outWidth;

    int h = newOpts.outHeight;

    //現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們設(shè)置為

    float hh = 800f;//這里設(shè)置高度為800f

    float ww = 480f;//這里設(shè)置寬度為480f

    //縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可

    int be = 1;//be=1表示不縮放

    if (w > h && w > ww) {//如果寬度大的話根據(jù)寬度固定大小縮放

    be = (int) (newOpts.outWidth / ww);

    } else if (w < h && h > hh) {//如果高度高的話根據(jù)寬度固定大小縮放

    be = (int) (newOpts.outHeight / hh);

    }

    if (be <= 0)

    be = 1;

    newOpts.inSampleSize = be;//設(shè)置縮放比例

    //重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了

    bitmap = BitmapFactory.decodeFile(srcPath, newOpts);

    return compressImage(bitmap);//壓縮好比例大小后再進(jìn)行質(zhì)量壓縮

    }

    ② 根據(jù)Bitmap圖片壓縮

    private Bitmap comp(Bitmap image) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);

    if( baos.toByteArray().length / 1024>1024) {//判斷如果圖片大于1M,進(jìn)行壓縮避免在生成圖片(BitmapFactory.decodeStream)時(shí)溢出

    baos.reset();//重置baos即清空baos

    image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//這里壓縮50%,把壓縮后的數(shù)據(jù)存放到baos中

    }

    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());

    BitmapFactory.Options newOpts = new BitmapFactory.Options();

    //開始讀入圖片,此時(shí)把options.inJustDecodeBounds 設(shè)回true了

    newOpts.inJustDecodeBounds = true;

    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);

    newOpts.inJustDecodeBounds = false;

    int w = newOpts.outWidth;

    int h = newOpts.outHeight;

    //現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們設(shè)置為

    float hh = 800f;//這里設(shè)置高度為800f

    float ww = 480f;//這里設(shè)置寬度為480f

    //縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可

    int be = 1;//be=1表示不縮放

    if (w > h && w > ww) {//如果寬度大的話根據(jù)寬度固定大小縮放

    be = (int) (newOpts.outWidth / ww);

    } else if (w < h && h > hh) {//如果高度高的話根據(jù)寬度固定大小縮放

    be = (int) (newOpts.outHeight / hh);

    }

    if (be <= 0)

    be = 1;

    newOpts.inSampleSize = be;//設(shè)置縮放比例

    //重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds 設(shè)回false了

    isBm = new ByteArrayInputStream(baos.toByteArray());

    bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);

    return compressImage(bitmap);//壓縮好比例大小后再進(jìn)行質(zhì)量壓縮

    }

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:Android圖片壓縮
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)