Android: 缩放图片文件引起的OOM异常


传输文件,或者设置头像,我们一般都会检查原始图片的大小,作缩放处理。

常用的Java版缩放图片代码:

view plaincopy to clipboardprint?
public Bitmap getZoomImage(Bitmap src, int desW, int desH)  
{  
    Bitmap desImg = null;  
    int srcW = src.getWidth(); // 原始图像宽  
    int srcH = src.getHeight(); // 原始图像高  
    int[] srcBuf = new int[srcW * srcH]; // 原始图片像素信息缓存  
      
    src.getPixels(srcBuf, 0, srcW, 0, 0, srcW, srcH);  
      
    // 计算插值表  
    int[] tabY = new int[desH];  
    int[] tabX = new int[desW];  
      
    int sb = 0;  
    int db = 0;  
    int tems = 0;  
    int temd = 0;  
    int distance = srcH > desH ? srcH : desH;  
    for (int i = 0; i <= distance; i++)  
    {/* 垂直方向 */ 
        tabY[db] = sb;  
        tems += srcH;  
        temd += desH;  
        if (tems > distance)  
        {  
            tems -= distance;  
            sb++;  
        }  
        if (temd > distance)  
        {  
            temd -= distance;  
            db++;  
        }  
    }  
      
    sb = 0;  
    db = 0;  
    tems = 0;  
    temd = 0;  
    distance = srcW > desW ? srcW : desW;  
      
    for (int i = 0; i <= distance; i++)  
    {/* 水平方向 */ 
        tabX[db] = (short) sb;  
        tems += srcW;  
        temd += desW;  
        if (tems > distance)  
        {  
            tems -= distance;  
            sb++;  
        }  
        if (temd > distance)  
        {  
            temd -= distance;  
            db++;  
        }  
    }  
      
    // 生成放大缩小后图形像素  
    int[] desBuf = new int[desW * desH];  
    int dx = 0;  
    int dy = 0;  
    int sy = 0;  
    int oldy = -1;  
      
    for (int i = 0; i < desH; i++)  
    {  
        if (oldy == tabY[i])  
        {  
            System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);  
        }  
        else 
        {  
            dx = 0;  
            for (int j = 0; j < desW; j++)  
            {  
                desBuf[dy + dx] = srcBuf[sy + tabX[j]];  
                dx++;  
            }  
            sy += (tabY[i] - oldy) * srcW;  
        }  
        oldy = tabY[i];  
        dy += desW;  
    }  
    // 生成图片  
    desImg = Bitmap.createBitmap(desBuf, desW, desH, Bitmap.Config.ARGB_8888);  
      
    return desImg;  

 public Bitmap getZoomImage(Bitmap src, int desW, int desH)
 {
  Bitmap desImg = null;
  int srcW = src.getWidth(); // 原始图像宽
  int srcH = src.getHeight(); // 原始图像高
  int[] srcBuf = new int[srcW * srcH]; // 原始图片像素信息缓存
  
  src.getPixels(srcBuf, 0, srcW, 0, 0, srcW, srcH);
  
  // 计算插值表
  int[] tabY = new int[desH];
  int[] tabX = new int[desW];
  
  int sb = 0;
  int db = 0;
  int tems = 0;
  int temd = 0;
  int distance = srcH > desH ? srcH : desH;
  for (int i = 0; i <= distance; i++)
  {/* 垂直方向 */
   tabY[db] = sb;
   tems += srcH;
   temd += desH;
   if (tems > distance)
   {
    tems -= distance;
    sb++;
   }
   if (temd > distance)
   {
    temd -= distance;
    db++;
   }
  }
  
  sb = 0;
  db = 0;
  tems = 0;
  temd = 0;
  distance = srcW > desW ? srcW : desW;
  
  for (int i = 0; i <= distance; i++)
  {/* 水平方向 */
   tabX[db] = (short) sb;
   tems += srcW;
   temd += desW;
   if (tems > distance)
   {
    tems -= distance;
    sb++;
   }
   if (temd > distance)
   {
    temd -= distance;
    db++;
   }
  }
  
  // 生成放大缩小后图形像素
  int[] desBuf = new int[desW * desH];
  int dx = 0;
  int dy = 0;
  int sy = 0;
  int oldy = -1;
  
  for (int i = 0; i < desH; i++)
  {
   if (oldy == tabY[i])
   {
    System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
   }
   else
   {
    dx = 0;
    for (int j = 0; j < desW; j++)
    {
     desBuf[dy + dx] = srcBuf[sy + tabX[j]];
     dx++;
    }
    sy += (tabY[i] - oldy) * srcW;
   }
   oldy = tabY[i];
   dy += desW;
  }
  // 生成图片
  desImg = Bitmap.createBitmap(desBuf, desW, desH, Bitmap.Config.ARGB_8888);
  
  return desImg;
 }

常用的Android版缩放图片代码:

view plaincopy to clipboardprint?
ContentResolver cr = this.getContentResolver();  
try 
{  
    InputStream in = cr.openInputStream(uri);  
    Bitmap bitmap = BitmapFactory.decodeStream(in);  
    try 
    {  
        in.close();  
    }  
    catch (IOException e)  
    {  
        e.printStackTrace();  
    }  
    if(null  == bitmap)  
    {  
        Toast.makeText(this, "Head is not set successful,Decode bitmap failure", 2000);  
    }  
    //原始图片的尺寸  
    int bmpWidth  = bitmap.getWidth();  
    int bmpHeight = bitmap.getHeight();  
      
    //缩放图片的尺寸  
    float scaleWidth  = (float) 40 / bmpWidth;  
    float scaleHeight = (float) 40 / bmpHeight;  
    Matrix matrix = new Matrix();  
    matrix.postScale(scaleWidth, scaleHeight);  
      
    //产生缩放后的Bitmap对象  
    Bitmap resizeBitmap = Bitmap.createBitmap(  
        bitmap, 0, 0, bmpWidth, bmpHeight, matrix, false);  
    bitmap.recycle();  
    //Bitmap to byte[]  
    byte[] photoData = Bitmap2Bytes(resizeBitmap);  
      
    //save file  
    String fileName = "/sdcard/test.jpg";  
    FileUtil.writeToFile(fileName, photoData);  
      
    //save photo check sum to db  
    DataCenter.GetInstance().ModifyIMMUser();  
    //refresh ImageView  
}  
catch (FileNotFoundException exp)  
{  
    exp.printStackTrace();  

  • 1
  • 2
  • 下一页

相关内容