Reputation: 3986
In my code, I have an image that convert bitmap, change the size and assigned a setDensity. Then I save in sd.
In this example, the final size would be 5x7 inches
This is my code:
private void SaveImage(Bitmap myBitmap) {
int TARGET_DENSITY = 300;
int PHYSICAL_WIDTH_IN_INCH = 5;
int PHYSICAL_HEIGHT_IN_INCH = 7;
Bitmap bmp = Bitmap.createBitmap(PHYSICAL_WIDTH_IN_INCH * TARGET_DENSITY, PHYSICAL_HEIGHT_IN_INCH * TARGET_DENSITY, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(myBitmap, 0, 0, new Paint());
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/aaa");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bmp.setDensity(TARGET_DENSITY);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
Log.i("control","Error");
}
}
The original size of the image is w:1024 x h:768px - 300dpi
The final size of the image generated by the app is: w:1500 x h:2100px - 72dpi
If using photoshop to change 72 dpi to 300 dpi, the result is correct (5x7inches), but the picture is output at 72dpi
Is there any way that the image is generated from the app with 300dpi?
I'm looking for information, but without success.
Thanks in advance
Upvotes: 0
Views: 3261
Reputation: 30985
There is nothing wrong with your image. It's 1500 x 2100 pixels, so if it is rendered on a 300dpi device, the image will be 5"x7".
The JPEG format doesn't store any resolution information, everything is in pixels. The 72dpi just comes from PhotoShop, it's a default resolution setting for converting pixels to inches. Why 72? You'd have to ask the PhotoShop team.
PhotoShop lets you set a DPI resolution so that when your horizontal and vertical rulers are displaying in inches, they will show the correct measurements based on your target output device. Changing the DPI won't change any properties of your image.
Upvotes: 1