圧縮される画像にテキストを書いています。
画像 sie2048 x 1536
何か px
最初に最初に書いてみました OutOfMemory Exception をスローします
次に、最初に圧縮しました1024 x 768
次に、画像にテキストを書きます
100KB
画像 KB を からに増やしました640KB
テキストを書いている間、私はcompromise
できるImage quality but not the text Quality
30
テキストに設定された圧縮品質 もdownsample
質問:
ImageSize(KB単位)を変更せずに、
write then compress
またはテキストへの処理はありますか?Compress then Write
画像サイズ (KB) をできるだけ小さくしたいですか?
また、 inSampleSize が 3 に設定されている場合は機能せず、 1 、 2 、 4 を使用した 2048 、 1024 、 512 の画像のみが出力として作成されます。縦横比を維持した 700px 前後のサイズの画像が必要です。
コード: .
StampingImageのメソッド
public void stampMyImage(String filePath, String text, Bitmap bitmap) {
String[] str = text.split("\n");
filePath = "/sdcard/geoTag/1imagelong_001_001_0002_0002_1135_130708144229.jpg";
bitmap = BitmapFactory.decodeFile(filePath);
if (bitmap != null) {
Bitmap dest = null;
try {
dest = bitmap.copy(bitmap.getConfig(), true);
} catch (OutOfMemoryError e1) {
Log.e("Exception", e1.getMessage());
e1.printStackTrace();
} catch (Error e) {
Log.e("Exception", e.getMessage());
e.printStackTrace();
}
Canvas cs = new Canvas(dest);
Typeface tf = Typeface.create("Verdana", Typeface.BOLD);
Paint tPaint = new Paint();
tPaint.setAntiAlias(true);
tPaint.setTextSize(40);
tPaint.setTextAlign(Align.LEFT);
tPaint.setTypeface(tf);
tPaint.setColor(Color.RED);
tPaint.setStyle(Style.FILL_AND_STROKE);
cs.drawBitmap(bitmap, 0f, 0f, null);
float textHeight = tPaint.measureText("yY");
int index = 0;
for (String Oneline : str) {
cs.drawText(Oneline, 20f, (index + 1) * textHeight + 25f,
tPaint);
index++;
}
try {
FileOutputStream fos = new FileOutputStream(
"/sdcard/timeStampedImage.jpg");
dest.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
通常圧縮の方法
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
int quality = 70;
myBitmapClose = BitmapFactory.decodeFile(imgUriClose.getPath(),options);
if (myBitmapClose != null) {
File sdImageMainDirectory = new File(imgUriClose.getPath());
try {
FileOutputStream fileOutputStream = new FileOutputStream(sdImageMainDirectory);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
myBitmapClose.compress(CompressFormat.JPEG, quality, bos);
if (bos != null) {
bos.flush();
bos.close();
}
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
画像サンプル
関連項目
私がたどったいくつかの便利なリンク