2013-06-25 14 views
7

こんにちは!Android。本当に大きなイメージ(ビットマップ)にテキストを書き込んで保存する方法

次の問題があります。かなり大きなイメージに長いテキストを書いてそこに保存する必要があります。

条件:

  1. 画像サイズ2000×3000を超えます。
  2. 画像を部分に分割することは許可されていません。これは、画像がエッジからエッジに書き込まれるためです。
  3. 追加:元の画像にテキストを書き込む必要があるため、ビットマップスケールまたはマトリックススケールは使用できません。
  4. を追加しました:私はビットマップのスケールを使用して、ユーザーに画像を表示する場合:メモリの不足とoptions.inSampleSize = 10;と何の問題

現在の問題:

私がしようビットマップエラーでこのイメージをロードすると、メモリの制限(VMバジェット)が発生します。

質問:

  1. メモリにロードすることなく、大きな画像の上にテキストにどのように?
  2. これのための既存のライブラリはありますか?

コード例:

// Step 1. Open image and load to Bitmap 
Bitmap bitmap = BitmapFactory.decodeFile(fileName); // runtime Exception like "VM budget" ! 
// or 
//Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.big_img_1); // as in previous runtime Exception like "VM budget" ! 
// or 
//Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream); // as in previous runtime Exception like "VM budget" ! 


// Step 2. Write text 
Paint paint = new Paint(); 
paint.setColor(Color.BLACK); 
paint.setTextSize(14); 

Canvas canvas = new Canvas(bitmap); 
canvas.drawText("some long long string over all image", 100, 100, paint); 

// Step 3. Save bitmap to file 
OutputStream fOut = new FileOutputStream(new File(fileName)); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
fOut.flush(); 
fOut.close(); 
/* 
    _________________________________________________ 
    |            | 
    |            | 
    |            | 
    |            | 
    |      IMG      | 
    |            | 
    |            | 
    | some long long string over all image | 
    |________________________________________________| 
    */ 
+0

画像が透明部分を持っているかどうか? – Plato

+0

透明使用していません – Andrei

+0

これに何か不運? – Adrian

答えて

0

まずあなたがキャンバスを使用することができ、

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

第二の画像に書き込み、保存するために大規模なビットマップをロードするには、次のヘルプを使用します

私はアプリでこれをします。キャンバスを使用します。

私のコードを編集しましたが、これは実際にバックグラウンドと他のものにいくつかの他のイメージを追加します。コードの

肉:

private static Bitmap getPoster(...) { 
Bitmap background = BitmapFactory.decodeResource(res, background_id) 
    .copy(Bitmap.Config.ARGB_8888, true); 
Canvas canvas = new Canvas(background); 
Typeface font = Typeface.createFromAsset(res.getAssets(), FONT_PATH); 
font = Typeface.create(font, Typeface.BOLD); 
Paint paint = new Paint(); 
paint.setTypeface(font); 
paint.setAntiAlias(true); 
paint.setColor(Color.WHITE); 
paint.setStyle(Style.FILL); 
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK); 
float fontSize = getFontSize(background.getWidth(), THE_QUOTE, paint); //You'll have to define a way to find a size that fits, or just use a constant size. 

paint.setTextSize(fontSize); 
canvas.drawText(THE_QUOTE, (background.getWidth() - paint.measureText(THE_QUOTE))/2, 
    background.getHeight() - FILLER_HEIGHT, paint); //You might want to do something different. In my case every image has a filler in the bottom which is 50px. 
return background; 
} 

はクラスでその独自のバージョンを入れて、それを画像IDと何かを養います。それは、あなたが望むものを何でもするためのビットマップを返します(イメージビューでそれを表示し、ユーザーに保存させて壁紙として設定させます)。

+0

この行の 'Bitmap background = BitmapFactory.decodeResource(res、background_id).copy(Bitmap.Config.ARGB_8888、true);'画像上に「VM budget」という例外が3000 x 2000以上を受け取る – Andrei

+0

このテクニックをうまく使いますここでは - http://developer.android.com/training/displaying-bitmaps/load-bitmap.htmlまたはMatrixクラスを使用して画像のサイズを小さくすることができます – blganesh101

+0

ビットマップスケールまたはマトリックススケールは使用できません。元の画像にテキストを書き込む必要があります(フルサイズ) – Andrei

1

これはあなたが探しているものではありませんが、参考になるかもしれません。

あなたのイメージが透明でない場合あなたはそれのためにRGB_565を指定して、それをロードする必要があるメモリの量を減らすことができますが、このように設定だ:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inPreferredConfig = Bitmap.Config.RGB_565; 
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, options); 
+0

フルコード 'BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inPurgeable = true; options.inInputShareable = true; options.inPreferredConfig = Bitmap.Config.RGB_565; ビットマップbm = BitmapFactory.decodeFile(filePath、options); 'まだ例外が発生しました。" java.lang.OutOfMemoryError:ビットマップサイズがVMの予算を超えています " – Andrei

関連する問題