Android自定义相机动作会返回低分辨率图像

时间:2019-08-14 08:28:54

标签: android

我从相机活动中获取的图像有问题。我是按照本教程操作的:https://developer.android.com/training/camera/photobasics.html#TaskPath,但是由于某种原因,我得到的是低分辨率图像(在我的手机320x240上,是从模拟的640x480像素获得的。)

我创建了Camera实例:

mCamera = CameraUtils(this).getCameraInstance()

然后创建文件:

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

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

然后将图像保存到文件:

        try {
            val fos = FileOutputStream(pictureFile)
            fos.write(data)
            fos.close()
        } catch (e: FileNotFoundException) {
            Log.d(tag, "camera File not found: ${e.message}")
        } catch (e: IOException) {
            Log.d(tag, "camera Error accessing file: ${e.message}")
        }
        intent.putExtra("file", pictureFile)
        intent.putExtra("idPhotoType", getPhotoTypeId())
        this.setResult(CommonStatusCodes.SUCCESS, intent)
        mCamera?.release()
        this.finish()

然后我从onActivityResult的意图中读取了它。

在创建Camera实例后,我还尝试添加一些代码来拍摄FULL HD图像,但是没有效果。

    mCamera = CameraUtils(this).getCameraInstance()
    val params = mCamera!!.parameters
    val sizes = params.supportedPictureSizes
    var w = 0
    var h = 0
    for (size in sizes) {
        if (size.width > w || size.height > h) {
            w = size.width
            h = size.height
        }

    }
    params.setPictureSize(1920, 1080)

谢谢您的建议。

1 个答案:

答案 0 :(得分:3)

尝试一下:

var params = mCamera!!.getParameters()
                    var supportedSizes = params.getSupportedPictureSizes()
                    sizePicture = supportedSizes.get(0)
                    params.setPictureSize(sizePicture.width, sizePicture.height)
                    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)) {
                        params.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
                    }

                    mCamera!!.setParameters(params)
                    mCamera!!.startPreview()
                    mCamera!!.autoFocus(object : Camera.AutoFocusCallback {
                        override fun onAutoFocus(success: Boolean, camera: Camera?) {
                            try {
                                camera!!.takePicture(null, null, mPictureCallbackRaw)
                            }
                            catch (ex:Exception)
                            {

                            }
                        }
                    })