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 或更多百万像素的相机,我out of memory期待。也许有人可以帮助我解决这个问题?

4

1 回答 1

1

来自相机的位图会很大,如果您只想将其保存为 900x900,那么您可能必须使用另一种读取它的方法(不是 MediaStore.Images.Media.getBitmap()),您可以在其中可以设置inSampleSize: http: //developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile (java.lang.String , android.graphics.BitmapFactory.Options)

在将 src 绘制到画布后,您还可以在 mark() 中回收它。目前您正在创建 2 个 900x900dp 位图,因此合并 2 个对 mark 的调用也可能会有所帮助。

于 2013-05-13T12:58:10.963 回答