将位图保存到文件中以上载到OCR

问题描述 投票:-1回答:1

我正在使用坐标裁剪图像,我正在设法得到一个位图。我想要做的是将位图保存在文件中,以便我可以上传到Microsoft的OCR路径。

我试图在不同函数的文件中输出流,然后返回文件。但该文件将返回null。

Bitmap bmpCroppedID = Bitmap.createBitmap(correctedBmp, idX, idY, idCroppedWidth, idCroppedHeight);

idImageView = findViewById(R.id.idImageView);
idImageView.setImageBitmap(bmpCroppedID);
try {
    filepathID= getImageFile(bmpCroppedID);
    text1.setText(file.getAbsolutePath());
} catch (IOException e) {
    e.printStackTrace();
}

Bitmap bmpCroppedVoltage = Bitmap.createBitmap(correctedBmp, voltageX, voltageY, voltageCroppedWidth, voltageCroppedHeight);

voltageImageView = findViewById(R.id.voltageImageView);
voltageImageView.setImageBitmap(bmpCroppedVoltage);
try {
    filepathVoltage= getImageFile(bmpCroppedVoltage);
    text2.setText(file2.getAbsolutePath());
} catch (IOException e) {
    e.printStackTrace();
}
private String getImageFile(Bitmap bitmap) throws IOException {

String timeStamp= new  SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

String imageName= "jpg "+timeStamp+"_";

File storageDir= getExternalFilesDir(Environment.DIRECTORY_PICTURES);

File imageFile= File.createTempFile(imageName,".jpg",storageDir);

FileOutputStream fileOutputStream = new FileOutputStream(imageFile);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);

fileOutputStream.flush();
fileOutputStream.close();

currentImagePath= imageFile.getAbsolutePath();
return currentImagePath;
}

图像正在被完美裁剪,我期待的是获取保存和压缩图像的路径,以便我可以将其发送到Microsoft的API。

android ocr bmp
1个回答
0
投票

您需要将此权限添加到AndroidManifest.xml文件中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

如果有,请检查您的指示:

File storageDir= getExternalFilesDir(Environment.DIRECTORY_PICTURES);

我相信这应该是:

File storageDir= new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES));

getExternalFilesDir返回路径字符串,而不是文件。

© www.soinside.com 2019 - 2024. All rights reserved.