【问题标题】:Camera Image is too small相机图像太小
【发布时间】:2016-08-08 08:14:15
【问题描述】:

最近几天,我遇到了120 x 160大小的Camera Image太小的问题。

场景是当我从相机捕获图像并将该图像转换为字节[]时,它变得太小了。

下面是我正在使用的代码:

 Bitmap unscaledBitmap  = (Bitmap) data.getExtras().get("data");
 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 unscaledBitmap  .compress(Bitmap.CompressFormat.PNG, 30, bytes);
 byte[] bytesDoc1 = bytes.toByteArray();

对此一无所知,但我已经尝试了许多关于 SO 的解决方案,但都没有为我工作。

请帮帮我。

【问题讨论】:

    标签: android bitmap android-camera bytearray android-camera-intent


    【解决方案1】:

    您需要指定相机意图的路径来存储真实图像。因为data.getExtras().get("data"); 只是从真实图像中获取缩略图。

    看看这段代码:

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
    // Create the File where the photo should go
    File photoFile = createImageFile();
    
    
    if (photoFile != null) {
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
       startActivityForResult(takePictureIntent, 1);
    }
    

    这是创建文件的方法:

    private File createImageFile() {
    
            long timeStamp = System.currentTimeMillis();
            String imageFileName = "NAME_" + timeStamp;
            File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );
    
            return image;
        }
    

    然后就可以读取创建图片的真实路径了。

    【讨论】:

    • 它会给出android.os.FileUriExposedException,如果你的targetSdkVersion = 24,我们必须使用FileProvider类
    【解决方案2】:

    你得到的只是缩略图。

    如果您想要真实的图像,最好给相机一个文件路径来存储图像。然后您可以在 onActivityResult 中使用该路径。

    【讨论】:

    • 感谢您的快速回复。但是我已经尝试过了,但它对我也不起作用。
    【解决方案3】:

    不确定在此问题上帮助您是否为时已晚。我也遇到了这个问题,只是想明白为什么那张照片这么小。 原因是在某些手机(LG Tribute 5)上,默认图片尺寸不知何故使用了最低的尺寸(160x120)。为了修复此错误,您必须从相机中获取支持的图片尺寸列表。 类似camera.getParameters.getSupportedPictureSizes(),并尝试选择您想要的最小分辨率,然后setPictureSize 选择您选择的最佳匹配尺寸。 这肯定会解决问题。

    【讨论】:

      【解决方案4】:

      我使用 scaleType fitXY 并且成功了

      <ImageView
          android:id="@+id/imgPicture"
          android:layout_width="70dp"
          android:layout_height="70dp"
          scaleType="fitXY"/>
      

      【讨论】:

        猜你喜欢
        • 2014-01-19
        • 2011-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-22
        相关资源
        最近更新 更多