从takePicture回调保存图像时Android的图像质量很差

时间:2014-11-25 02:38:09

标签: java android android-camera

我已经看过很多关于Android相机图像不佳的其他堆栈答案,但它们似乎都来自那些抓住缩略图然后进行缩放的人。

我的问题是,即使保存在图书馆中的图像质量也很差。它的颜色暗淡且像素化更多。

这是我的回调,我将图像保存到库中。当我保存时,你能看到我丢失图像质量的原因吗?

这是回调

  @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
        MediaScannerConnection.scanFile(getBaseContext(), new String[]{pictureFile.getPath()}, new String[]{"image/png"}, null);

这是我获取outputMediaFile的地方

private static File getOutputMediaFile(int type){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "PumpUp");
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".png");
            Log.d(DEBUG_TAG,mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".png");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

更新:

位图数据给我的宽度和高度为320.你知道为什么它会缩小吗?

更新2:

此代码将打印出320W 240H

private Camera.PictureCallback mPicture = new Camera.PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            Bitmap imageBitmap = BitmapFactory.decodeByteArray(data,0,data.length);
            Log.d(DEBUG_TAG,imageBitmap.getWidth() + "  h " + imageBitmap.getHeight());
}

为什么我的图像在收到回调之前会缩小尺寸?

3 个答案:

答案 0 :(得分:1)

解决方案在这里:https://stackoverflow.com/a/10605793/3324388我需要将图片大小设置为相机的参数

答案 1 :(得分:0)

这种情况正在发生,因为您只获得图像的缩略图表示。

要获取图片的确切大小,您必须将URI作为EXTRA_OUTPUT参数传递给相机意图

 //create file at path where you like
 File photo = new File();
 URI resultUri = Uri.fromFile(photo);
 intent.putExtra(MediaStore.EXTRA_OUTPUT, resultUri);
 //this resultUri would be  your final image

答案 2 :(得分:0)

在开发网站here上明确说明如下:

  

注意:此缩略图来自"数据"可能对图标有好处,但是   不是很多。处理完整尺寸的图像需要更多的工作。

要获得完整尺寸的照片,您需要这样做:

以下是启动相机时需要调用的方法:

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}    

以下是创建具有唯一名称的新文件的方法:

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}