简体   繁体   English

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

[英]Saving a bitmap into a file to upload to OCR

I am cropping an image using coordinates and I am managing to get a bitmap.我正在使用坐标裁剪图像,并且正在设法获取位图。 What I would like to do is save the bitmap in a file so that I can upload the path to Microsoft's OCR.我想要做的是将位图保存在一个文件中,以便我可以将路径上传到 Microsoft 的 OCR。

I have tried to output stream's in a file in a different function then returning the file.我试图在不同函数的文件中输出流,然后返回该文件。 But the file would return null.但该文件将返回 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;
}

The image is being cropped perfectly and what I am expecting is to get the path to the save and compressed image so that I can send it to Microsoft's API.图像被完美裁剪,我期待的是获取保存和压缩图像的路径,以便我可以将其发送到 Microsoft 的 API。

You will need to add this permission to your AndroidManifest.xml file:您需要将此权限添加到您的 AndroidManifest.xml 文件中:

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

If that is in place, check your instruction:如果已到位,请检查您的说明:

File storageDir= getExternalFilesDir(Environment.DIRECTORY_PICTURES);

I believe that this should be:我认为这应该是:

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

getExternalFilesDir returns a path string, not a file. getExternalFilesDir 返回路径字符串,而不是文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM