Android: mPrevCallback to JPG, results in black pictures

458 views Asked by At

I am trying to capture the preview of the camera on a surfaceview ,to save it as a JPEG in the internal memory. I found some code here on this site, that does mostly I want but saves the image to the SD Card. I changed that, and came up with the following code.

Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback()
{
    @Override
    public void onPreviewFrame( byte[] data, Camera Cam ) {
        //Log.d(TAG, "FRAME");
        Camera.Parameters parameters = Cam.getParameters();
        int format = parameters.getPreviewFormat();
        //Log.d(TAG, "FORMAT:" + format);
        //YUV formats require more conversion
        if (format == ImageFormat.NV21 || format == ImageFormat.YUY2 || format == ImageFormat.NV16) {
            int w = parameters.getPreviewSize().width;
            int h = parameters.getPreviewSize().height;
            // Get the YuV image
            YuvImage yuv_image = new YuvImage(data, format, w, h, null);
            // Convert YuV to Jpeg
            Rect rect = new Rect(0, 0, w, h);
            ByteArrayOutputStream output_stream = new ByteArrayOutputStream();
            yuv_image.compressToJpeg(rect, 100, output_stream);
            byte[] byt = output_stream.toByteArray();
            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream("/data/data/com.example.max.camtest/files/test"+System.currentTimeMillis()+".jpg");
                outStream.write(byt);
                outStream.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }


        }
    }
};

The preview is shown on the surfaceview and the mPrevCallback is triggered.It successfully saves pictures that have diffrent sizes (250~500Kb) but they are all black. When I try to capture a picture with the camera.takePicture function is it also black.

What Am I doing wrong? How can I debug this? Thanks!

1

There are 1 answers

2
rcorbellini On

Use this intent to take picture

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File f = new File(android.os.Environment.getExternalStorageDirectory(), AppInfo.getInstance().getCurrentLoginUserInfo().getId()+".jpg");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 1);

and on Your Activity Result.... Note Bitmap bitmap = getScaledBitmap(uri.getPath(), 200, true); 200 is your max image size.

if(requestCode == 1)
        {
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
            final String imgPath = base + "/" +AppInfo.getInstance().getCurrentLoginUserInfo().getId()+".jpg";
            File file = new File(imgPath);
            if (file.exists()) 
            {
                Uri uri = Uri.fromFile(file);

            Log.d(TAG, "Image Uri path: " + uri.getPath());

            Bitmap bitmap = getScaledBitmap(uri.getPath(), 200, true);

        }}

This method ll return image bitmap after resizing it-

private Bitmap getScaledBitmap(String imagePath, float maxImageSize, boolean filter) {

FileInputStream in;
BufferedInputStream buf;

try {
    in = new FileInputStream(imagePath);

    buf = new BufferedInputStream(in);
    Bitmap realImage = BitmapFactory.decodeStream(buf);


    float ratio = Math.min(
            (float) maxImageSize / realImage.getWidth(),
            (float) maxImageSize / realImage.getHeight());

    int width = Math.round((float) ratio * realImage.getWidth());
    int height = Math.round((float) ratio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, height, filter);
    return newBitmap;

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

return null;
}

Now you have scaled bitmap image.

Hope this ll help you.