【问题标题】:How can I save output image from camera directly to file without compression?如何将相机的输出图像直接保存到文件而不进行压缩?
【发布时间】:2016-06-09 15:33:56
【问题描述】:

压缩文件再发短信后,又重新压缩,很断断续续

我使用 Intent 来调出相机。我得到结果并调出 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 倍。我可以直接保存我从相机调用中获得的Bitmap而不压缩,发送图片后文件将被删除。用户实际拍摄照片,然后点击默认短信应用上的发送按钮。

【问题讨论】:

    标签: android camera sms


    【解决方案1】:

    要直接保存图像而不压缩使用AndroidBmpUtil:

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

    【讨论】:

    • 效果更好,但文件仍然非常小而且非常不稳定
    • 此方法将图像保存为原样。如果图像很小且不稳定,则可能是图像本身。
    • 我已经弄清楚了一些事情并根据我的理解设置了代码,但是当我运行应用程序时它没有运行并给我错误Error:(39, 30) error: cannot find symbol method getWidth(),`错误:(40, 31)错误:找不到符号方法getHeight()`,Error:(62, 18) error: cannot find symbol method getPixels(int[],int,int,int,int,int,int)这些都在AndroidBmpUtil如果你知道如何解决这个问题请告诉我。
    【解决方案2】:

    代替

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

    使用

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

    JPEG 是一种有损格式,因此结果不连贯。

    【讨论】:

    • 谢谢。我试过了,它们仍然波涛汹涌。我认为我进入位图照片的内容可能已经不稳定
    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 2011-10-29
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多