I am facing this problem for 2 days now. I am making a 2D game via Canvas and it has a PNG file which I am drawing on the canvas by drawBitmap(), and it works fine.
But for Per-Pixel-Collision detection, I am using getPixel(), but it's always returning 0.
I am creating the bitmap in th following way-
Bitmap bmp= BitmapFactory.decodeResource(context.getResources(), R.drawable.image);
//image is a PNG file
but following always gives 0-
Toast.makeText(getContext(),""+bmp.getPixel(x,y), Toast.LENGTH_SHORT).show();
//x and y are within the boundary
I even tried the following, but same results-
BitmapFactory.Options opt=new BitmapFactory.Options();
opt.inMutable=true;
Bitmap bmp= BitmapFactory.decodeResource(context.getResources(), R.drawable.image,opt);
Being a beginner,I don't have much understanding of bitmaps and color scheme, so, reason on why this is happening and any solution would be of great help.
My project is stalled midway for this problem.
Thank you
Got a solution on my own. Its rather a workaround, than a solution, but it works.
Just pass a BitmapFactory.Options object to BitmapFactory.decodeStream like the one below.
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap source = BitmapFactory.decodeStream(context.getResources(), R.image,opt);
Related
I have two copies of same image. One of them is inside the resource folder (default drawable) of the app and the other one is on the external storage.
I get the Bitmap with the following codes:
// Get from storage
BitmapFactory.decodeFile(image.getAbsolutePath());
// Get from resource
BitmapFactory.decodeResource(getResources(), R.drawable.image);
However, they result in different sizes in ImageView with wrap_content in height and width. How can I solve this?
This behavior is because of the implementation of BitmapFactory. During the call in decodeResourceStream from decodeResource, it will assign a BitmapFactory.Options wtih inDensity set to DisplayMetrics.DENSITY_DEFAULT if BitmapFactory.Options is null.
On the other hand, decodeFile is passed through setDensityFromOptions which return immediately if BitmapFactory.Options is null.
Therefore, one of the solutions is to scale the density of decodeFile by the following code.
BitmapFactory.Options option = new BitmapFactory.Options();
option.inDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), option);
If you want to do it in reverse, you can use the following code.
InputStream inputStream = context.getResources().openRawResource(R.drawable.image);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Notice that there will be "Constants and Resource Type Mismatches", but it can still be compiled. decodeResource also call openRawResource as InputStream, so this is fine.
So, I'm updating an old e-book reader. The problem I'm facing is the size of the bitmaps (the pages). The way it works is: the app downloads a file, extract it, decode it with our DRM then I can access all pages. The thing is, each page its a 35mb bitmap.
I need to reduce this by a lot. I can't convert the file to any other format.
I tried this:
byte[] rawBytes = intToByteArray(b.getByteCount());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bit = BitmapFactory.decodeByteArray(rawBytes, 0,rawBytes.length, options);
But the Bitmap keeps returning null, and for what I gathered its when the Bitmap facorty can't decode the bitmap.
So, there is any other way to reduce the impact of this big bitmaps ?
I tried searching for a relevant example but found none, so I'll try to be as precise as I can.
Back in the gingerbread era I made a draw bitmap to canvas code that worked flawlessly and here it is.
//this one is drawing the background for my picture
mBackground = BitmapFactory.decodeResource(getResources(), R.drawable.base);
//setting a new canvas
mComboImage = new Canvas(mBackground);
//adding another bitmap to the canvas, don't worry about the size or the i variables
mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.base);
mBackImage=Bitmap.createScaledBitmap(mBackImage, size, 105, false);
mComboImage.drawBitmap(mBackImage, 100f+1f*size*i, 170f, null);
mBackImage.recycle();
//this is setting my background for an icon to the completed image
Drawable d =new BitmapDrawable(getResources(),mBackground);
aaa.setBackground(d);
anyways the code doesn't seem to fit now. One problem I have faced is converting the bitmap into mutable which you can check out here if you are stuck on it as I was for a while.
My problem is that the background is perfectly drawn but the mBackImage doesn't show up at all.
What worries me more is that this used to work perfectly before.
I really have tried searching for a newer explanation but haven't really found any on stackoverflow so
I drew bitmap as background image for my app as well. I used: setBackgroundDrawable() instead of setBackground() in your last statement.
Also, when preparing the bitmap, I set two options:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable=true;
bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.image1), options);
You can give it a try.
By following this link, I have written the following code to show a large image bitmap from sdcard.
try {
InputStream lStreamToImage = context.getContentResolver().openInputStream(Uri.parse(imagePath));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(lStreamToImage, null, options);
options.inSampleSize = 8; //Decrease the size of decoded image
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(lStreamToImage, null, options);
} catch(Exception e){}
image.setImageBitmap(bitmap);
But it is not returning the bitmap(I mean it returns null). In logcat it is showing the below message repeatedly
08-02 17:21:04.389: D/skia(19359): --- SkImageDecoder::Factory returned null
If I will comment the options.inJustDecodeBounds line and rerun it, it works fine but slowly. The developer guide link I provided above says to use inJustDecodeBounds to load bitmaps efficiently.
Please tell me where I am doing wrong.
inJustDecodeBounds does not load bitmaps. That's the point of it. It loads the dimensions of the bitmap without loading the actual bitmap so you can do any pre-processing or checking on the bitmap before you actually load it. This is helpful is you, say, were having memory issues and you needed to check if loading a bitmap would crash you program.
The reason your bitmap might be loading slowly is because it's probably very large and SD cards are very slow.
EDIT:
From the documentation:
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
Edit 2:
Looking at your code with the example provided by Google, it looks like you are doing relatively the same thing. The reason it's returning null is possibly your InputStream has been modified in the first decoding and thus not starting at the beginning of the bitmap's memory address (they use a resource ID rather than InputStream.
From the code you supplied here, here's what I've figured. You are ALWAYS setting a sample size to 8 regardless of what the first decoding gives you. The reason Google decodes the first time is to figure out what the actual size of the bitmap is versus what they want. They determine that the bitmap is ZxZ dimensions and they want YxY dimensions, so they calculate the samplesize that they should use from the second decoding. You are not doing this. You are simply retrieving the dimensions of the bitmap and not using them. THEN, you set the sample size to a hard-coded 8, swapping it to a hard-coded ARGB_4444 bitmap, then decoding the full bitmap in to memory. In other words, these three lines are not being used:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(lStreamToImage, null, options);
Setting inJustDecodeBounds merely gives you the bitmap's dimensions without putting the bitmap in to memory. It doesn't make it more efficient. It's meant to allow you to load bitmaps in a smaller memory space if they are too big because you can pre-decide what size it should be without decoding the whole thing).
The reason decoding the bitmap is slow might merely be a CPU thing. Depending on the size of your bitmap, you're loading the bitmap from an InputStream from the SDcard which is a slow operation in itself.
In my app, the bitmap is drawn as if the color is some lower quality type. If i load up the background image using the gallery app, it loads just fine and does not look like it's super low quality. The code i am using to load and draw my images is simple:
//Code for initializing the Bitmap
Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.none), (int) (canvas.getWidth() * compression), (int) (canvas.getHeight() * compression), true);
//...
//Code for drawing this Bitmap
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), null);
If nothing in the code tells you what is wrong, i made an image comparing what the image actually looks like on a computer or other image viewer, and what it looks like in the app.
question is somewhat similar to Bad image quality after resizing/scaling bitmap
try disabling scaling, resize in an offscreen bitmap and make sure that Bitmap is 32 bits (ARGB888):
Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
another good and complete answer about image scaling/processing can be found at Quality problems when resizing an image at runtime