Sunil Lohar
Sunil Lohar

Reputation: 2242

canvas.drawBitmap gives two different results

I m taking screenshot of android screen and Draw another image on captured image (watermark) using canvas.drawBitmap,

it all giving proper result when i copy the Watermark(another image - say visit.jpg) image on SD card, the captured image has width 500pixel, where visit.jpg has width- 339pixels,

File file = new File( Environment.getExternalStorageDirectory() + "/visit.jpg");
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

but i dont want to copy the watermark image into sdcard, i want the image from res folder should get draw on captured image, so i used

Bitmap visit = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.visit);

and draw on canvas and saved, it get saved, but the Watermark image(visit.jpg) gets stretch and its size gets larger than 339pixels. why ?

my code is :

        View v1 = findViewById(android.R.id.content).getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap screenshot = Bitmap.createBitmap(v1.getDrawingCache());

        Bitmap cropimg = Bitmap.createBitmap(screenshot, 50, 0, screenshot.getWidth()-100, 592);
        v1.setDrawingCacheEnabled(false);


        Bitmap Rbitmap = Bitmap.createBitmap(cropimg).copy(Config.ARGB_8888, true);


        Bitmap visit = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.visit); // image accessed from drawable
        Canvas canvas = new Canvas(Rbitmap);       

        File file = new File( Environment.getExternalStorageDirectory() + "/visit.jpg"); // the image copied on sd card
        Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

        canvas.drawBitmap(visit, 0, 570, null); // if i put myBitmap instead of visit i.e.  canvas.drawBitmap(myBitmap, 0, 570, null); then visit.jpg image gets draw properly with 339pixel width without strtch
        canvas.save();

Upvotes: 0

Views: 537

Answers (1)

Neil
Neil

Reputation: 468

EDIT: I forgot an easier way, put the images in /res/drawable-nodpi folder.

Android will automatically scale the resource image depending on your screen density. The default drawable folder correspond to the mdpi so if you have a hdpi device the image will be scaled up. You can put the image into the /res/raw/ folder then open it like:

Uri imageUri = Uri.parse("android.resource://your.app.package/raw/visit");
InputStream is = context.getContentResolver().openInputStream(imageUri);
Bitmap bmp = BitmapFactory.decodeStream(is, null, opts);

Upvotes: 1

Related Questions