如何在不压缩的情况下将相机的输出图像直接保存到文件中? [英] How can I save output image from camera directly to file without compression?

查看:178
本文介绍了如何在不压缩的情况下将相机的输出图像直接保存到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

压缩文件然后将其发送文本后,它会再次重新压缩并且非常不稳定

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

我使用Intent调出相机。

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;

}

这很好用,只是文本中产生的图像不稳定。如果我发送最后出现在图库中的同一张图片,则会将其向下转换,结果在接收端要好100倍。我可以直接保存从相机调用中获取的位图而不进行压缩吗,发送图片后文件将被删除。用户实际上拍摄了照片,然后在默认的短信应用程序上单击了发送按钮。

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.

推荐答案

直接保存图像而不进行压缩使用 AndroidBmpUtil

To save an image directly without compressing use AndroidBmpUtil:

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

这篇关于如何在不压缩的情况下将相机的输出图像直接保存到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆