Uri imageUri = ...;
mGPUImage = new GPUImage(this);
mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView));
mGPUImage.setImage(imageUri); // this loads image on the current thread, should be run in a thread
mGPUImage.setFilter(new GPUImageSobelEdgeDetection());
// Later when image should be saved saved:
mGPUImage.saveToPictures("GPUImage", "ImageWithFilter.jpg", null);
由于Android中没有BufferedImage,您可以自己完成所有基本操作:
Bitmap b = ...
width = b.getWidth();
height = b.getHeight();
stride = b.getRowBytes();
for(int x=0;x<b.getWidth();x++)
for(int y=0;y<b.getHeight();y++)
{
int pixel = b.getPixel(x, y);
// you have the source pixel, now transform it and write to destination
}
Color.rgb(r1,g1,b1)
函数从RGB数组中组合像素来获取像素值。 - reflog另一个选择是使用OpenCV,它在Android上有很好的实现。
Imgproc.Sobel()
方法接受一个'Mat'类型的图像作为输入,可以轻松地从资源或位图中加载。输入Mat应该是灰度图像,也可以使用opencv创建。
Mat src = Highgui.imread(getClass().getResource(
"/SomeGrayScaleImage.jpg").getPath());
然后在其上运行Sobel边缘检测器,并将结果保存在新的Mat中。如果您想保持相同的图像深度,则可以这样做...
Mat dst;
int ddepth = -1; // 目标深度。-1保留源深度
int dx = 1;
int dy = 1;
Imgproc.Sobel(src, dst, ddepth, dx, dy);
对于在Android Studio中进行gradle构建,您可以从不同的地方获取为Java构建的opencv库,但我也提供最新的构建。在build.gradle文件中,您可以添加依赖项,如下所示...否则,它会有点棘手。
dependencies {
compile 'com.iparse.android:opencv:2.4.8'
}
如果您使用Eclipse,则可以查看Opencv网站以了解有关在Android上使用Opencv的详细信息:http://opencv.org/platforms/android.html
http://code.google.com/p/kanzi/source/browse/java/src/kanzi/filter/SobelFilter.java
这个程序没有依赖于Swing/AWT或任何其他库。它直接操作图像像素,速度很快。
结果可以在这里看到(向下滚动):