2013-05-13 42 views
0

我試圖捕獲圖像,並從onActivityResult設置水印方法是從我的代碼片段。捕獲位圖並添加水印

Private void savingCapturedImage() { 
    long date = System.currentTimeMillis(); 
    Date data = new Date(date); 
    File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", "mobiliskaita.JPG"); 
    Uri imagePath = Uri.fromFile(file); 

    try { 
     Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imagePath); 
     System.out.println("bitmap: " + bitmap.getWidth() + " " + bitmap.getHeight()); 
     file.delete(); 
     bitmap = mark(bitmap, String.valueOf(data), 100, 200, 100, false); 
     bitmap = mark(bitmap, TheGlobals.partneriaiValue, 100, 310, 100, false); 


     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     bitmap.recycle(); 
     File fileOutput = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", photoName()); 
     fileOutput.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(fileOutput); 
     fo.write(bytes.toByteArray()); 
     fo.flush(); 
     fo.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


    private Bitmap mark(Bitmap src, String watermark, int x, int y, int size, boolean underline) { 
    int w = src.getWidth(); 
    int h = src.getHeight(); 
    Point _p = new Point(); 
    _p.x = x; 
    _p.y = y; 

    final float scale = getResources().getDisplayMetrics().density; 
    int p = (int) (900 * scale + 0.5f); 

    Bitmap result = Bitmap.createScaledBitmap(src, p, p, true); 

    Canvas canvas = new Canvas(result); 
    canvas.drawBitmap(src, 0, 0, null); 

    Paint paint = new Paint(); 
    paint.setColor(Color.BLACK); 
    paint.setTextSize(R.dimen.default_text_size); 
    paint.setAntiAlias(true); 
    paint.setUnderlineText(underline); 
    canvas.drawText(watermark, _p.x, _p.y, paint); 

    return result; 
} 

它的工作,但後來我有8個或更多的百萬像素CAMER,我得到out of memory厚望。也許有人可以幫我解決這個問題?

回答

1

如果您只是將它保存爲900x900,則相機的位圖會很大,那麼您可能需要使用另一種方法來讀取它(而不是MediaStore.Images.Media.getBitmap()) ,你可以在其中設置inSampleSize:http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String,android.graphics.BitmapFactory.Options)

你也可以在mark()中將src繪製到畫布後回收src。目前您正在創建2個900x900dp位圖,因此將2個調用合併爲標記也可能有所幫助。

+0

我試圖做你喜歡說,它幾乎不錯,但我有另一個問題 [鏈接] http://www.part.lt/img/eb4e149bc162f8ce4ccb9249fd1ca22c384.jpg,whatch圖片,得到一個正常的圖片和它的另一個1/2更小。 :) – Justinas 2013-05-14 06:43:28

+0

好吧,解決了這個問題,另一個最後一個!然後我捕捉圖像並保存,它改變方向。 – Justinas 2013-05-14 10:52:16