我正在寻找Android中应用程序的简单示例,该应用程序使用opencv将彩色图像转换为灰度。我正在尝试使用代码,但是在此行Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);
后应用程序崩溃。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img=(ImageView)findViewById(R.id.imageView1);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);
Utils.bitmapToMat(bmp, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY,4);
Utils.matToBitmap(tmp, bmp);
img.setImageBitmap(bmp);
}
真正快速的方法。没有opencv。
private Button button;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
image = (ImageView)findViewById(R.id.imageView1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
image.setColorFilter(filter);
}
});
我刚刚尝试过,对我有用。
编辑:查看此处的链接以获取官方文档。http://developer.android.com/reference/android/graphics/ColorMatrix.html#setSaturation(浮子)
您的布局尚未布局,而您处于onCreate()
,这就是为什么bmp.getWidth()
和bmp.getHeight()
返回 0 的原因。
一种方法是覆盖onWindowFocusChanged()
private ImageView img;
private boolean isFullyInitialized = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView1);
}
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if (hasFocus && !isFullyInitialized)
{
isFullyInitialized = true;
openCvCode();
}
}
private void openCvCode()
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);
Utils.bitmapToMat(bmp, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY,4);
Utils.matToBitmap(tmp, bmp);
img.setImageBitmap(bmp);
}
问题已解决:
public class MainActivity extends Activity {
ImageView img;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView1);
bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.lena);
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
{
Log.e("TEST", "Cannot connect to OpenCV Manager");
}
}
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Mat tmp = new Mat(bitmap.getWidth(), bitmap.getHeight(),CvType.CV_8UC1);
// Convert
Utils.bitmapToMat(bitmap, tmp);
Mat gray = new Mat(bitmap.getWidth(), bitmap.getHeight(),CvType.CV_8UC1);
// Conver the color
Imgproc.cvtColor(tmp, gray, Imgproc.COLOR_RGB2GRAY);
// Convert back to bitmap
Mat destination = new Mat(gray.rows(),gray.cols(),gray.type());
Imgproc.adaptiveThreshold(gray, destination, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 4);
Utils.matToBitmap(destination, bitmap);
img.setImageBitmap(bitmap);
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
}