Image size is too small Azure Face API Android - android

I am trying to use the Azure Face API on android. I am capturing an image from the device camera and then converting it to an InputStream to be sent to the detect method. I keep getting the error "com.microsoft.projectoxford.face.rest.ClientException: Image size is too small"
I checked the documentation and the image size is 1.4Mb which is within the 1Kb-4Mb range. I don't understand why it isn't working.
Bitmap bitmap = cameraKitImage.getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
bitmapdata = bos.toByteArray();
new FaceTask().execute(new ByteArrayInputStream(bitmapdata));
Face[] faces = faceServiceClient.detect(inputStreams[0], true, false, null);

Somehow your file is being compressed more than 1Kb or if it is small in actual size already. Try to save it somewhere in the drawable or assets folder and open it in the input stream.

Related

Convert YUV imageformat to PNG

I was struggeling to get YuvImage to a png imageformat on an Android 5.0.1 device where the png showed up as green images. On a Android 5.1.1 this did not happend, and the images was showing just fine.
After some time I found out that there is a bug in Android 5.0.1 which makes the images that are converted appear green. This was fixed in Android 5.1.1
However, does anyone know about a solution in order to make this work on devices that has not got this fix?
I don't think there is a way to workaround the bug because in my experience the images are already green when generated by the system, and it is not a problem of the conversion to PNG.
I see that you are using the Camera 2 API from your comment response, and since you are using YUV format I believe you are trying to save images from the continuous feed from the camera (as opposed from full resolution picture taking). If that is the case, I'll suggest using the older Camera API if at all possible, as I haven't seen a device that does not work when capturing preview images in YUV format (NV21), which can easily converted to a PNG, although having to go through a JPEG step:
YuvImage yuvImage = new YuvImage(nv21bytearray, ImageFormat.NV21, width, height, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, os);
byte[] jpegByteArray = os.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegByteArray, 0, jpegByteArray.length);
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/imagename.png");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
with nv21bytearray being the NV21 byte array returned by the old camera API onPreviewFrame(...) method.

Compress a 1080p video pngs into small size

I am developing an app which generate .png images from a 1080p video. But the pngs are in MBs, and even my app is crashing due to large size of pngs. I want to compress or something like that to reduce the size of each png. I have done a lot of methods like createScaledBitmap, or compress(CompressFormat.PNG, 20, stream); or Bitmap.createBitmap(source, 0, 0, source.getWidth(),source.getHeight(), m, true); also searched a lot of methods.
But it's not reducing the size as much as I want. it still remains in 2.2+ MB per png.
Any idea other than these. Thanks.
I am also building an app which deals with high resolution images but on server side. I am compressing and re-sizing the image on server side by using thumbnailator-0.4.8.jar. Example
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage bImageFromConvert = ImageIO.read( in );
BufferedImage newImage = Thumbnails.of(bImageFromConvert).size(213, 316).asBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(newImage, "jpg", baos);
baos.flush();
retVal = baos.toByteArray();
baos.close();

change default "picture no available" icon in Android

I've wrote an app that encrypt/decrypt pictures. When the encrypted image is stored, in the image gallery we can see the "picture no available" icon for this encrypted image. I would like to change this to default icon, but I don't know where is this default stored...
thanks!
EDIT
What I am trying to do now, is with BitMapFactory convert the encripted file to a bitmap. The icon it's not possible to change and EXIF algorithm is not exactly what I want.
With this code I obtain a black bitmap, what I would like to do is to obtain a bitmap from the encripted file that shows colour pixels. Any idea?
Bittmap bitmap=BitmapFactory.decodeFile("/sdcard/abc.jpg");
Bitmap bm = bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 30, bos);
bos.toByteArray();
FileOutputStream ft = new FileOutputStream(new File("/sdcard/ab.jpg"));
bos.writeTo(ft);
bos.flush();
ft.flush();
bos.close();
ft.close();
Since your app encrypts images I guess that you want them all to be viewable only by your app. So why don't you just change the file extension to something other so that the Android Image gallery won't recognize the file as image and try to display it.
This is outside of your control. Default Gallery appliaction (and probably 1000 others on Google Play) display their own image when parsing fails the way they want.

Why my Image Quality is not good while sending it to server in My Android App?

This is my code to send Image on Server . my image is going to server but not in good quality.
photo = (Bitmap) data.getExtras().get("data");
image_from_camera.setImageBitmap(photo);
if(photo != null)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 50, stream);
//Here if I increase the size of quality like up to
//photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
//then i am getting BAD REQUEST ERROR i.e request exceeds size limit.
byte[] byte_arr = stream.toByteArray();
image_string_chore_detail = Base64.encodeToString(byte_arr,Base64.DEFAULT);
System.out.println(
"IMAGE STRING IN CHORE DETAILS:: "+image_string_chore_detail);
}
So please help me what to do....
Use png-32 format for good quailty.
change the format in photoshop and by this you will improve the quailty.
Its because you are reducing the image quality to half the original in this line while compressing,
photo.compress(Bitmap.CompressFormat.JPEG, 50, stream);
The 50 in this line means half. Maybe u have to try it with 100 which will provide you full quality without any loss.
The string is too large for your server to handle. You need to change the settings in the server to allow a larger file.

Android: Upload JPG image without losing EXIF

I'm uploading JPG image as byte[] but Bitmap strips of EXIF before converting to byte[]. How do I upload raw jpg without converting it to Bitmap?
File imagefile = new File(filepath + "DSC00021.jpg");
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis); // EXIF info lost
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPG, 100, baos);
byte[] data = baos.toByteArray();
p.s. I don't want to use any 3rd party library. ExifInterface can only write to file and not streams/byte arrays.
Above code will not work most cases. In case if you want to decode large size image you will get "out of memory error". Decode using bitmpafactory options.
Convert the file to bitmap by
Bitmap bi = BitmapFactory.decode(filepath + "DSC00021.jpg");
You can specify options too, look at API documentation
Or if you want to exchange the meta data from one file to another, sanselan will probably be the best choice. This would be much helpful when you manipulating the image, for example re-size.
The sample code will guide you in a right direction.

Categories

Resources