2

Is it possible to save a canvas as image at a higher resolution than the device? Is there some way of scaling the images up with out losing the pixel density?
I am working on an app which let's you draw and add text and images on to the canvas. Currently when I am saving the canvas as an image, the resolution of that image is dependent upon the device. I want this to be independent of the device and also the image should be saved at very high res (say A3 300dpi).

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Jayadev
  • 201
  • 1
  • 8
  • 1
    yes, when drawing on your device use scaled-down Canvas (canvas.scale(0.5f, 0.5f) for example) and when drawing on the exported image use unchanged Canvas so the size wil be two times bigger – pskink Jul 30 '15 at 06:29
  • Thank you for the suggestion. Will the quality of image be comprimised in that case? – Jayadev Jul 30 '15 at 06:31
  • it all depends what you are drawing on the Canvas – pskink Jul 30 '15 at 06:34
  • Is it possible to have a chat with you ? – Jayadev Jul 30 '15 at 06:38
  • Accept an answer. All your questions should be solved? – daemmie Jul 30 '15 at 07:44
  • if you look at this question: http://stackoverflow.com/questions/24395076/canvas-generated-by-canvg-is-blurry-on-retina-screen/ you will see how to increase the resolution of a canvas element. Instead of `window.devicePixelRatio` use something like `3` and it will be 3x the standard dpi. – ericjbasti Jul 31 '15 at 03:26

2 Answers2

2

It is not possible to save canvas as heigh resolution image. Image resolution means number of pixels which forms the image. In canvas case canvas size will be the resolution of target image. One possible way to achieve this

  • Store all user drawn element as an objects
  • Create empty image with target resolution
  • Get Canvas of the target image with Canvas c= new Canvas(image)
  • Calculate scale factor with respect to your device canvas size and new canvas size
  • Now scale all user drawn objects with the calculated scale factor
  • Redraw all object on bigger canvas and export image

Above logic can be performed once user click export/save button from UI

Amit Padekar
  • 259
  • 1
  • 6
  • Thank you for the help :) Could please explain how to save user elements as individual objects? – Jayadev Jul 30 '15 at 06:35
  • 1
    For user added text, store custom object with properties location x and y, text size. Transform location x and y with scale factor and scale text size with scale factor and draw text on bigger canvas with updated values – Amit Padekar Jul 30 '15 at 06:59
1

You can't be independent of the device. Because the max allowed size differs on each device.

Take a look at getMaximumBitmapWidth() and getMaximumBitmapHeight() of the Canvas

But you can draw in Bitmaps with the Canvas. But this has been answered multiple times. For example here: https://stackoverflow.com/a/5664047/5038873

A solution for you problem might be to tile your target resolution in sub images, limited by the max canvas size. Then draw with the canvas on those images.

In a following step you can put those bitmaps together, by creating a Bitmap with your target resolution an copy the pixels of the other images into it.

Community
  • 1
  • 1
daemmie
  • 6,361
  • 3
  • 29
  • 45
  • Thank you :). Is it possible to save the image in A3 300 dpi from an android device? – Jayadev Jul 30 '15 at 06:16
  • I think the maximum I have seen was 16384x16384px on the Nexus 6 and also on the Galaxy S6. But I'm not 100% sure. – daemmie Jul 30 '15 at 06:20
  • 1
    Both Nexus 6 and s6 have a resolution of 2560 x 1440 pixels – Jayadev Jul 30 '15 at 06:22
  • Yes display resolution but the getMaximumBitmapWidth() returns other values. The max Canvas Size does not depend on the screen size. – daemmie Jul 30 '15 at 06:24
  • But often devices with higher screen resolution can use a higer max canvas size. – daemmie Jul 30 '15 at 06:25
  • 1
    I have added some more informations which may help you to achieve what you want. Without loosing any quality. – daemmie Jul 30 '15 at 06:45
  • @Jayadev have you done A3 300 dpi from an android device. i also have same problem. any solution.. – Ram Oct 08 '16 at 14:04