How can I save output image from camera directly to file without compression?

泪湿孤枕 提交于 2020-01-06 15:52:38

问题


After compressing the file and then texting it, it recompresses again and is very choppy

I use Intent to bring up the camera. I get the result and bring up Intent to send as text.

private void takePic() {
    Intent cameraIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 2);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 2) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        File file = writebitmaptofilefirst("route_image",photo);
        Uri uri = Uri.fromFile(file);
        fileToDelete = file;
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.putExtra("address", "8001111222");
        sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
        sendIntent.setType("image/jpg");
        startActivityForResult(sendIntent,3);
    }
    else if (requestCode == 3){
        if (fileToDelete.exists()) fileToDelete.delete();
    }
}

public static File writebitmaptofilefirst(String filename, Bitmap source) {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File mFolder = new File(extStorageDirectory + "/temp_images");
    if (!mFolder.exists()) {
        mFolder.mkdir();
    }
    OutputStream outStream = null;
    File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
    if (file.exists()) {
        file.delete();
        file = new File(extStorageDirectory, filename + ".jpg");
        Log.e("file exist", "" + file + ",Bitmap= " + filename);
    }
    try {
        outStream = new FileOutputStream(file);
        source.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("file", "" + file);
    return file;

}

This works great except the resulting image texted is choppy. If I send the same pic that ends up in the Gallery it converts that down and the result is 100 times better at the receiving end. Can I save the Bitmap I get from the camera call directly without compression, the file will be deleted after the sending of the picture. The user actually takes the pic and then hits the send button on the default sms app.


回答1:


To save an image directly without compressing use AndroidBmpUtil:

new AndroidBmpUtil().save(source, file);



回答2:


Instead of

source.compress(Bitmap.CompressFormat.JPEG, 90, outStream);

use

source.compress(Bitmap.CompressFormat.PNG, 100, outStream);

JPEG is a lossy format hence the choppy result.



来源:https://stackoverflow.com/questions/37730365/how-can-i-save-output-image-from-camera-directly-to-file-without-compression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!