2012-03-07 58 views
2

我已閱讀關於此問題的這些帖子,但我的情況是有點不同,因爲我不顯示位圖,但只是從原始數據後處理圖像。 首先,我打電話ImageProcessing.rgbToBitmap(data,width, height);這將返回一個位圖對象。然後我分別執行這3個功能。位圖大小超過虛擬機預算

  1. 旋轉位圖
  2. 添加水印重疊的位圖
  3. 的右下角

所有3種方法被稱爲將創建一個回報的位圖對象,它可能會導致爲位圖

  • 添加日期崩潰,因爲我試圖每1000毫秒保存一個圖像!有時保存的圖像可能由於內存錯誤而失真。

    我在下面發佈我的代碼,非常感謝您的任何建議。我不想在拍攝的圖像質量上妥協。 (需要保持分辨率)

    public static Bitmap addWatermark(Resources res, Bitmap source) { 
         int w, h; 
         Canvas c; 
         Paint paint; 
         Bitmap bmp, watermark; 
    
         Matrix matrix; 
         float scale; 
         RectF r; 
    
         w = source.getWidth(); 
         h = source.getHeight(); 
    
         // Create the new bitmap 
         bmp = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565); 
    
         paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG 
           | Paint.FILTER_BITMAP_FLAG); 
    
         // Copy the original bitmap into the new one 
         c = new Canvas(bmp); 
         c.drawBitmap(source, 0, 0, paint); 
    
         // Load the watermark 
         watermark = BitmapFactory.decodeResource(res, R.drawable.watermark); 
         // Scale the watermark to be approximately 10% of the source image 
         // height 
         scale = (float) (((float) h * 0.80)/(float) watermark.getHeight()); 
    
         // Create the matrix 
         matrix = new Matrix(); 
         matrix.postScale(scale, scale); 
         // Determine the post-scaled size of the watermark 
         r = new RectF(0, 0, watermark.getWidth(), watermark.getHeight()); 
         matrix.mapRect(r); 
         // Center watermark 
         matrix.postTranslate((w - r.width())/2, (h - r.height())/2); 
    
         // Draw the watermark 
         c.drawBitmap(watermark, matrix, paint); 
         // Free up the bitmap memory 
         watermark.recycle(); 
    
         return bmp; 
        } 
    
    public static Bitmap addDate(Bitmap src, String date) { 
         int w = src.getWidth(); 
         int h = src.getHeight(); 
         //Bitmap result = Bitmap.createBitmap(w, h, src.getConfig()); 
         Bitmap result = src; 
         Canvas canvas = new Canvas(result); 
         canvas.drawBitmap(src, 0, 0, null); 
    
         Paint paint = new Paint(); 
         paint.setColor(Color.rgb(255, 185, 15)); 
         paint.setTextSize(20); 
         paint.setAntiAlias(true); 
         canvas.drawText(date, w - 200, h - 20, paint); 
    
         return result; 
        } 
    public static Bitmap rotate(Bitmap src, int rotation) { 
    
         int width = src.getWidth(); 
         int height = src.getHeight(); 
    
         // create a matrix for the manipulation 
         Matrix matrix = new Matrix(); 
         // rotate the Bitmap 
         matrix.postRotate(rotation); 
    
         // recreate the new Bitmap, swap width and height and apply 
         // transform 
         Bitmap rotatedBitmap = Bitmap.createBitmap(src, 0, 0, width, height, 
           matrix, true); 
         return rotatedBitmap; 
    
        } 
    
  • +0

    回收()可能是你的答案... android對此很嚴格.. – Joe 2012-03-07 07:52:46

    回答

    0

    請先嚐試: 變化

    // Copy the original bitmap into the new one 
    c = new Canvas(bmp); 
    c.drawBitmap(source, 0, 0, paint); 
    

    // Copy the original bitmap into the new one 
    c = new Canvas(bmp); 
    bmp.recycle(); //here or below 
    c.drawBitmap(source, 0, 0, paint); 
    //below bmp.recycle(); 
    

    這裏:

    canvas.drawText(date, w - 200, h - 20, paint); 
    
    return result; 
    

    canvas.drawText(date, w - 200, h - 20, paint); 
    result.recycle(); 
    return result; 
    

    這裏

    Bitmap rotatedBitmap = Bitmap.createBitmap(src, 0, 0, width, height, 
           matrix, true); 
    return rotatedBitmap; 
    

    Bitmap rotatedBitmap = Bitmap.createBitmap(src, 0, 0, width, height, 
           matrix, true); 
    return rotatedBitmap; 
    rotatedBitmap.recycle(); 
    

    添加這一切(再循環();),也許這已經足夠了,也嘗試代碼較小的位圖,帶看它是否仍然崩潰(像50px乘50px)。

    不,他們沒有辦法增加手機的虛擬機。

    +0

    有沒有更簡單的方法來做到這一點?因爲我需要在整個位圖上應用水印。我不知道如何在較小的塊上處理(應用水印)... – humansg 2012-03-07 13:24:49

    +0

    圖像的數量(寬x高) – Bigflow 2012-03-07 14:00:16

    +0

    它取決於用戶設置的配置。我基於設備的支持動態生成一個列表。由於我使用的是圖像預覽尺寸,因此我從三星s1看到的最高分辨率是1280 x 720. – humansg 2012-03-07 16:06:06

    相關問題