我正在尝试从手机上传照片并对其进行线性过滤。但我得到了错误信息。这些是我程序的文件:
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.m.opencvjava">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomeActivity"></activity>
</application>
</manifest>
MainActivity:
package com.m.opencvjava;
public class MainActivity extends AppCompatActivity {
private final int SELECT_PHOTO = 1;
private ImageView ivImage, ivImageProcessed;
Mat src;
static int ACTION_MODE = 0;
static final int MEAN_BLUR = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivImage = findViewById(R.id.ivImage);
ivImageProcessed = findViewById(R.id.ivImageProcessed);
Intent intent = getIntent();
if (intent.hasExtra("ACTION_MODE"))
{
ACTION_MODE = intent.getIntExtra("ACTION_MODE", 0);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK)
{
try {
final Uri imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
src = new Mat(selectedImage.getHeight(), selectedImage.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(selectedImage, src);
switch (ACTION_MODE)
{
case MEAN_BLUR:
Imgproc.blur(src, src, new Size(3,3));
break;
}
Bitmap processedImage = Bitmap.createBitmap(src.cols(),
src.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(src, processedImage);
ivImage.setImageBitmap(selectedImage);
ivImageProcessed.setImageBitmap(processedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_load_image) {
Intent photoPickerIntent = new
Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,
SELECT_PHOTO);
return true;
}
return super.onOptionsItemSelected(item);
}
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
break;
default:
super.onManagerConnected(status);
break;
}
}
};
@Override
protected void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10,
this,
mOpenCVCallBack);
}
}
HomeActivity:
package com.m.opencvjava;
import org.opencv.core.Mat;
public class HomeActivity extends AppCompatActivity {
Button bMean = (Button) findViewById(R.id.bMean);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
bMean = findViewById(R.id.bMean);
bMean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent (getApplicationContext(),
MainActivity.class);
i.putExtra("ACTION_MODE", MainActivity.MEAN_BLUR);
startActivity(i);
}
});
}
}
我必须说,我已经在安装了OpenCV 3软件包管理器的Galaxy Note 5手机上运行了其他应用程序并得到了同样的错误,但我想我去了设置并检查了相机权限(针对该应用程序)和问题解决了。我做了同样的事情并检查了此应用程序的存储权限但无法解决问题!
我不知道是什么问题?