2012年6月26日 星期二

Android 擷取當前屏幕、縮小屏幕、儲存影像

擷取當前屏幕並且縮小

public Bitmap getScaleBitmap() {
     Bitmap resizeBmp= null;
     int bmpWidth = 0;
     int bmpHeight = 0;
     float scaleRate = 0.5f;

     final View view = getChildAt(0) ;
     view.setDrawingCacheEnable(true);
     view.buildDrawingCache(true);
     Bitmap tempBmp = view.getDrawingCache();     
     bmpWidth = view.getWidth();   
     bmpHeight = view.getHeight();  
     //產生resize後的Bitmap對象
     Matrix matrix=new Matrix();
     matrix.postScale(scaleRate , scaleRate);
     Bitmap resizeBmp=Bitmap.createBitmap(tempBmp , 0, 0, bmpWidth, bmpHeight, m​​atrix, true);
     
     if(!tempBmp.isRecycled())
     {           
            tempBmp.recycle();
            tempBmp = null;
            System.gc();  //系統強制garbage recycle 
     }

     view.destroyDrawingCache();
     view.setDrawingCacheEnable(false);
    
     return resizeBmp;
}
  儲存影像
 public void savePNGPicture(Bitmap src, String dest)
{
 FileOutputStream fos = null;

 try
 {
  fos = new FileOutputStream(dest);

  if(fos != null)
  {
   Log.d(TAG, "savePicture path = "+dest+" format = png"); 
   src.compress(Bitmap.CompressFormat.PNG, 90, fos);
   fos.flush();
   fos.close();
  }
 }
 catch (FileNotFoundException e)
 {
  e.printStackTrace();
 }
 catch (IOException e)
 {
  e.printStackTrace();
 }
}

沒有留言:

張貼留言