Unable to catch an exception while decoding a base64 string on Kotlin - android

I'm trying to convert a base64 string in a BitMap, in order to show a photo in an ImageView. I'm failing because I'm receiving a bad string (how to fix this problem is not the scope of this topic), so I tried to handle this situation with a try-catch block.
Well, this block doesn't work because there's no exception throwed. As you can see from the Logcat in the lower part of the below image,the Base64 object (or the BitmapFactory one) just write a log about the failure (D/skia: failed to create image decoder with message 'unimplemented'), but don't launch any exception. There's no trace of my PHOTO-tagged log instead.
How could I do to manage this situation manually?
(I'm sorry if you'll find my english strange or difficult to read. I'm not mothertongue, but any help or criticism about it is well accepted)

Is it a compressed image encoded to Base64? (like .jpg or .png)
If so, the format of the image is not supported by the image decoder.
Otherwise, if it's raw data encoded in Base64, you should use Bitmap.createBitmap() to create a Bitmap.

Related

Retrieve data from raw image taken with camera2API

I have used the official sample for using the Camera2 API to capture a RAW sensor frame. The code is in java but I transformed it to Kotlin with the help of Android Studio. I tested it, and I'm capable of taking and saving a dng picture to my phone. Not a problem so far.
But what I really want is to be able to retrieve some information about the picture, I don't care about saving it. I want to do the processing directly with my smartphone.
What I tried so far, is to get the byte array of the image.
In the function dequeueAndSaveImage, I retrieve the image from a ImageReader : image = reader.get()!!.acquireNextImage().
I suppose that is here that I have to process the image. I tried to log the image.width, image.height and the image.planes.count and there was no problem.
By the way, since the format is RAW_SENSOR, the image.planes.count is 1, corresponding to a single plane of raw sensor image data, with 16 bits per color sample.
But when I'm trying to log the image.planes[0].buffer.array().size for example, I'm getting a FATAL EXCEPTION: CameraBackground with java.lang.UnsupportedOperationException.
And if I'm trying to log the same thing, but in the function that saves the image to a dng file, I'm getting another type of error : FATAL EXCEPTION: AsyncTask #1 with java.lang.UnsupportedOperationException
Am I even going the right way in order to retrieve information about the image? For example the the intensity of the pixels, the average, the standard deviation for each channel of color, etc...
EDIT : I think that I found the problem, although not the solution.
When I log image.planes[0].buffer.hasArray(), it returns false, that's why calling array() throws an exception.
But then, how do I get the data from the image?

How to understand this exception raised in an OpenCV program

I have got the following exception in my OpenCV program. I had the following image in my computer and I moved it to my mobile phone and read it by Mat imageRead = Highgui.imread("/mnt/sdcard/Pictures/2im00.png");
Then I tried to convert its color space to HSV using the following statement, and got the exception on this statement.
Imgproc.cvtColor(imageRead, hsvImage, Imgproc.COLOR_RGB2HSV);
But the exception does not seem to tell me anything more than that it is in the function cvtColor, or I can't read the encoded information there.
So the question is that how do I find out why I am getting this exception?
Is there any coded information there, like some codes (like scn==3 or scn==4 or error:-215 or depth etc etc), which I can browse somewhere to find out why I am getting the exception?
Most probably, Assertion failure occurs because you are passing an empty image to the cvtColor function.
Or
The Mat image you are passing is not an CV_8U or CV_32F format.
I agree with Miki's comment for more details follow this link How to interpret c++ opencv Assertion error messages due to an error in cvtColor function?

How to get mime type from a bitmap object in android?

I want to get mime type of a bitmap object.
Actually I have compressed a bitmap using
bitmap.compress (Bitmap.CompressFormat.PNG , 100, stream);
Now I want to cross check the format, outBounds and memory space size. But I don't wanna do a lengthy or a tedious task eg. to convert it to back to stream and then convert it again to BItmap using BitmapFactory with options.
Like Dmitry suggests, if what you have is a compressed image in a stream, then you do need to decode it using a BitmapFactory to tell what type it is. (Technically, you could also read the stream yourself and run your own ad hoc file format checks on it, but I assume you'd prefer to use existing tools rather than rolling your own.)
One optimization you can and should make is to set BitmapFactory.Options.inJustDecodeBounds to avoid needlessly decoding the entire image if you just want to know what type it is. After decoding, the outMimeType property should give you the type of the image.
As for the literal question in your title, it doesn't make sense. A Bitmap has no MIME type — it's just a container for an array of pixels. You can compress the bitmap into a stream using various formats, like JPEG, PNG or WEBP, but that just gives you a compressed copy of the image.

Android : Bitmap : From parcel : Out of memorry exception

I am using a bundle to send a bitmap from one application to another.
And i retrieve the bitmap from the bundle for use in a different application.
The specific use of bundle was necessary in this place.
And when i read it out i get a OUT OF MEMORY EXCEPTION.
bitmap = (Bitmap)receivedmsg.getData().getParcelable("myobject");
Any suggestions ?
It means what it says. The image you serialized is too big to be read back into memory in the other app. The fastest fix is what M Mohsin Naeem alludes to: you need to make the image smaller! Do so in the app that sends the image. For example, if that app is reading from a file, you can set it to down-sample the image to a smaller size.
Also consider whether you really need to send the image this way, or whether you could save it to the SD card, and then process it without reading into memory.

How To Fill Bitmap Data in ARGB Format in Android LabelView

I'm working with Android and I really need a fast way to get a bitmap of format BGRA to be filled in ARGB.
One thing I aslo want to let u know that The Data comes in byte[] format and I have to convert in int[] format also.
Can AnyOne Tell me How to do this ...
Thanks in Advance
If you want to load a Bitmap by bytestream, you can use Bitmap.decodeStream. You could then use getPixel(s?) to get the int array. This is one way I know how to do this, probably not the fastest though. A faster way would be to convert bytes to int, if your byte array is nothing but pixeldata this won't be too hard.
BGRA to ARGB can be done with bitshifting quite fast.
A nice source you would probably like:
https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx
The fastest way I think would be to do it in native code using the NDK. I've been considering to use it for image processing for some time but didn't get the chance yet. So I don't know much about it (i.e. how you would access your byte buffer) but you could start from the Plasma sample for bitmap processing in JNI.

Categories

Resources