【问题标题】:Android Poor Image Quality When Saving Image From takePicture callback [duplicate]从takePicture回调保存图像时Android图像质量差[重复]
【发布时间】:2014-11-25 02:38:08
【问题描述】:

我查看了一堆其他关于糟糕的 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());
}

为什么我的图片在得到回调之前就被缩小了?

【问题讨论】:

    标签: java android android-camera


    【解决方案1】:

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

    【讨论】:

    • 感谢您给我正确的链接+1
    【解决方案2】:

    它在开发网站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;
    }
    

    【讨论】:

    • 解决方案在我的问题的顶部。
    • 请再次完整阅读问题。他没有使用 Intent 拍照,而是使用带有真实 Camera 对象的表面视图
    【解决方案3】:

    发生这种情况是因为您只获得了图像的缩略图..

    要获得精确尺寸的图像,您必须将 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
    

    【讨论】:

    • 有趣的是,我在任何文档中都没有读到这方面的内容。你在哪里找到这个的?:)
    • @Aggressor 我们正在这样做
    • @Aggressor 解决了你的问题吗?如果可以,您可以接受并投票赞成答案吗?
    • 是的,刚开始工作,我很快就会尝试
    • 嗯,这似乎不起作用,保存到画廊的图像质量仍然很差
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多