Hey,
this is the code I used. It will do Out Of Memory for big images, so
perhaps it need some changes. But for my purpose (demo version of app
with small watermarked images) it was just fine. You can download
whole android project here: http://halmi.sk/watermark.zip
private void doWatermark() {
//create a new file, specifying the path, and the filename
File root = new
File(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/");
//create subdirectory
try {
if (!root.exists()) {
root.mkdir();
}
} catch (Exception e) {}
String source = root.getAbsolutePath() + "/car.jpg";
Log.i("Watermark", source);
//file which we want to save as.
String filename = System.currentTimeMillis()+".jpg";
try {
Log.i("Watermark", "appending watermark");
BitmapFactory.Options options = new BitmapFactory.Options();
//most of the time there's not enough memory to fit both
original image
//watermark and merged image, so we set output image as half of
original
options.inSampleSize = 2;
//load source image
Bitmap capture = BitmapFactory.decodeFile(source);
//prepare empty bitmap that will serve as output
Bitmap result = Bitmap.createBitmap(capture.getWidth(),
capture.getHeight(), Config.ARGB_8888);
//flush it into canvas
Canvas canvas = new Canvas(result);
//now we're going to draw on canvas - add watermark over
original image
canvas.drawBitmap(capture, 0, 0, null);
//R.drawable.watermark is our watermark image
Bitmap watermark = BitmapFactory.decodeResource(getResources(),
R.drawable.watermark);
//draw watermark into center of canvas
canvas.drawBitmap(watermark,
capture.getWidth()/2 - watermark.getWidth()/2,
capture.getHeight()/2 - watermark.getHeight()/2,
null);
//output file
File file = new File(root, filename);
file.createNewFile();
//prepare output stream
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bos = new
BufferedOutputStream(fileOutputStream);
//compress bitmap as jpeg
result.compress(CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
Log.i("Watermark", "image saved to file: " +
file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en