【问题标题】:take and save picture with specified size (in KB-s)拍摄并保存指定大小的照片(以 KB-s 为单位)
【发布时间】:2014-04-23 17:52:00
【问题描述】:

在我的应用程序中,我拍照并以指定的方式保存。我的手机是 Nexus 4,相机非常强大,每张照片大约 2.31MB 我怎样才能将照片保存在 60KB 中? 我可以用代码来做吗?以及如何?

File newdir = new File(dir);
         if(!newdir.exists()){
             newdir.mkdirs();
         }
        picturesCount++;
        String file = dir+picturesCount+".jpg";
        imagePath = file;
        File newfile = new File(file);
        try {
            newfile.createNewFile();
        } catch (IOException e) {}       
        Uri outputFileUri = Uri.fromFile(newfile);
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

【问题讨论】:

  • 我不认为你会得到图像的大小,直到你把它保存在某个地方,你所能做的就是降低质量,你会得到显着的差异
  • 又如何降低其质量?
  • 到目前为止你有一些代码吗?
  • 我已经添加了代码部分

标签: android


【解决方案1】:

当您使用相机并拍照时,您可以按照以下方法执行任务以降低图像质量并键入JPEG或PNG(PNG takes more space than JPEG)所以只需使用值(60 current out of 100)看看大小的区别。

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    InputStream in = new ByteArrayInputStream(data);

    BitmapFactory.Options options = new BitmapFactory.Options();
    Bitmap preview_bitmap = BitmapFactory.decodeStream(in, null, options);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    preview_bitmap.compress(Bitmap.CompressFormat.JPEG, 60, stream);
    byte[] byteArray = stream.toByteArray();

    FileOutputStream outStream = null;
    try {
        outStream = new FileOutputStream(new File(
                Environment.getExternalStorageDirectory(), "Image1.jpg"));
        outStream.write(byteArray);
        outStream.close();
    } catch (FileNotFoundException e) {
        Log.d("CAMERA", e.getMessage());
    } catch (IOException e) {
        Log.d("CAMERA", e.getMessage());
    }
}

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多