通过意图拍摄照片会严重降低照片质量?

时间:2019-07-12 07:06:27

标签: android android-camera

我有一个正在Rock960(Android SBC)上运行的应用程序。单击按钮,我调用dispatchTakePictureIntent(View View)函数来初始化应用程序拍照所需的所有内容。我正在使用Logitech C270网络摄像头。它没有自动对焦功能,但我已经确定了从相机到要拍摄的对象的距离,当我看到相机预览时,一切都聚焦了。

现在的问题是:单击我的按钮时,可以看到对象已对准焦点。但是,当我单击“拍照”图标时,该应用程序会拍照并向我显示所拍摄的照片,并且我可以取消,接受或重拍。但是,它显示的照片的质量往往非常差。好像它试图再次拍照,但是没有成功。

以下是我应用中的相关代码:

public void dispatchTakePictureIntent(View view) {
    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) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    BuildConfig.APPLICATION_ID + ".provider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, 1024);
        }
    }
}

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 = 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();

    Log.e(TAG, "mCurrentPhotoPath is = " + mCurrentPhotoPath);

    return image;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1024) {
        if (resultCode == Activity.RESULT_OK) {
            Log.e(TAG, "result from camera is OK!");
            //  Prepare the Tesseract Files
            prepareTessData();
            startOCR();
        } else if (resultCode == Activity.RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Result canceled.", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Activity result failed.", Toast.LENGTH_SHORT).show();
        }
    }
}

private void startOCR(){
    try{
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inSampleSize = 6;
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, options);
        String result = this.getText(bitmap);
        textView.setText(result);
    }catch (Exception e){
        Log.e(TAG, e.getMessage());
    }
}

清单文件:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

现在,我不确定这里发生了什么。可能是因为我的相机没有自动对焦?该代码在Android平板电脑和手机上可以正常工作,但是与我的Rock960配合使用。

1 个答案:

答案 0 :(得分:0)

数据中提供的图像仅是enter image description here。要获取完整尺寸的图像,您需要提供一个完全合格的文件名,相机应用程序应在其中保存照片。文档中对此进行了描述,并提供了示例代码。