gpt4 book ai didi

java - 如何使用 OpenCV 和 Android Studio 从手机加载图像?

转载 作者:行者123 更新时间:2023-12-01 09:00:58 25 4
gpt4 key购买 nike

我正在 android studio 中使用 opencv 开发一个 android 应用程序,我按照《OpenCV Android 编程示例》一书中的说明编写了从手机加载图像的代码。但是我无法在 ImageView 上显示它代码与书上一模一样,主要代码如下:

 @Override
onCreate(...){...}
..........

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.read_img) {
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"),
SELECT_PICTURE);
return true;
}
return super.onOptionsItemSelected(item);
}
public void onActivityResult(int requeseCode,int resultCode,Intent data){
if(resultCode==RESULT_OK){
if(requeseCode==SELECT_PICTURE){
Uri selectedImgUri=data.getData();
selectedImagePath=getPath(selectedImgUri);

loadImage(selectedImagePath);//the method to get the image
displayImage(sampledImage);//to display the image on ImageView
}
}
}
............
getPath(String path){.....}//it works fine

public void loadImage(String path){
Mat originalImage=Imgcodecs.imread(path);
Mat rgbImage=new Mat();
Imgproc.cvtColor(originalImage, rgbImage, Imgproc.COLOR_BGR2RGB);

//to reshape the image.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width=size.x;
int height=size.y;
sampledImage=new Mat();
double downSampleRatio=calculateSubSampleSize(rgbImage,width,height);
Imgproc.resize(rgbImage,sampledImage,new Size(),downSampleRatio,downSampleRatio,Imgproc.INTER_AREA);

//to rotate the image,seems not a problem in this case.
try {
ExifInterface exif = new ExifInterface(selectedImagePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
switch (orientation){
case ExifInterface.ORIENTATION_ROTATE_90:
sampledImage=sampledImage.t();
Core.flip(sampledImage, sampledImage, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
sampledImage=sampledImage.t();
Core.flip(sampledImage, sampledImage, 0);
break;}
} catch (IOException e){
e.printStackTrace();
}
}


public void displayImage(Mat image){
Bitmap bitmap=Bitmap.createBitmap(image.cols(),image.rows(),Bitmap.Config.RGB_565);
Utils.matToBitmap(image,bitmap);
ImageView imageView2=(ImageView)findViewById(R.id.imageView2);
imageView2.setImageBitmap(bitmap);
}
private static double calculateSubSampleSize(Mat srcImage, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = srcImage.height();
final int width = srcImage.width();
double inSampleSize = 1;

if(height>reqHeight|| width > reqWidth) {
// Calculate ratios of requested height and width to the raw height and width
final double heightRatio = (double) reqHeight / (double) height;
final double widthRatio = (double) reqWidth / (double) width;
// Choose the smallest ratio as inSampleSize value, this will //guarantee final image with both dimensions larger than or //equal to the requested height and width.
inSampleSize = heightRatio<widthRatio ? heightRatio :widthRatio;
} return inSampleSize;

}}

该应用程序可以运行,但是当我在手机上选择图像并单击“确定”时,它崩溃了,logcat 显示:

org.opencv.imgcodecs.Imgcodecs.imread(Imgcodecs.java:118)
at com.example.administrator.opencvdemo.MainActivity.loadImage(MainActivity.java:142)
at com.example.administrator.opencvdemo.MainActivity.onActivityResult(MainActivity.java:118)
at android.app.Activity.dispatchActivityResult(Activity.java:5652)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3600)

显然这是 loadImage() 方法的问题,但我搜索谷歌和到处都没有找到好的答案,我尝试修复它,但这是同样的问题。有人知道如何解决它吗?它让我太疯狂了。非常感谢!

最佳答案

您是否检查过 Manifest 上的应用程序权限?我从外部存储加载文件时遇到同样的问题。

我添加了<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />给我的AndroidManifest.xml 。从 Android 6 开始,您需要在运行时请求权限。

我处理了 MainActivity 上的权限,如下所示

package test;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

public class MainActivity extends Activity implements OnClickListener, ActivityCompat.OnRequestPermissionsResultCallback {

private String TAG = "MainActivity";

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
} break;

default:
{
Log.i(TAG, "OpenCV loaded FAILURE");
super.onManagerConnected(status);
} break;
}
}
};

public void onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, mLoaderCallback);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String[] PERMISSIONS = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};

if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, 1);
}
// Do work
}
}

希望对你有帮助。

关于java - 如何使用 OpenCV 和 Android Studio 从手机加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41669206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com