i-Droid i-Droid - 3 months ago 8
Android Question

Captured Image is blurry in my android camera app

I have developed my own camera app with some features and camera is working good.
The issue are captured images were blurred on SD card and image quality and sizes are varying on different mobiles even camera pixels are good and same.
I don't know what is the problem on capturing and saving images in app. I have followed guidelines from developer page and few samples from github.

I couldn't find exact solutions for my problem but some samples implemented bitmap and those pic's are good in quality. I have no idea about bitmap

Any idea to solve this problem would be helpful for my further development.

Code for pictureCallBack

Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
camera.takePicture(shutterCallback, rawPictureCallBack, null, jpegPictureCallback);
}
};


Thread to save images

FileOutputStream outStream = null;
/**
* Write to SD Card
*/
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/MyCamera");
dir.mkdirs();

String fileName = String.format("%d.png", System.currentTimeMillis());
File outFile = new File(dir, fileName);

outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
outStream.flush();
outStream.close();
runOnUiThread(new Runnable() {
@Override
public void run() {
retrieveLastImage();
showCustomAlert();
}
});
Log.d(DEBUG_TAG, "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath());
refreshGallery(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}

Answer

This code looks ok, I just wonder, that you save the file with .png extension: usually only jpeg is available...

Did you call setPictureSize()? You need to call it, as the default size is unspecified and may be too small - blurred. You need to always call setPreviewSize() as well.

It is tricky to choose the correct sizes. Always choose one from the sizes returned by getSupportedPictureSizes() and getSupportedPreviewSizes(). The preview size must have the same aspect ratio as the SurfaceView, where you show the preview, or the preview will be stretched. Choose the picture size according to your needs.

I had lot of troubles with the camera. You will do the best if you exactly follow the Google documentation, or otherwise the camera behaves strangely. It might work on one device, but crash on another. I tried, for example, to set the sizes in activity's onCreate method, but that did not work.