Android图像处理简单例子
有人短我要jni库,直接把工程分享出来得了。之前主要是觉得实现不佳,并且重点再算法,所以没附工程==。
内容太少,附点代码&截图^^。
1
)JoinImage.java
Java native类,native接口&辅助方法。
- public class JoinImage {
-
- static {
- System.loadLibrary("JoinImage");
- }
-
-
- private static final String TAG = "JoinImage";
-
-
- public static final String PATH = Environment.getExternalStorageDirectory()
- .toString() + "/" + TAG + "/";
-
-
-
-
- public static boolean hasSDCard() {
- return Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED) ? true : false;
- }
-
-
-
-
- public static void saveBitmap(String bitName, Bitmap mBitmap) {
-
-
- if (!hasSDCard()) {
- return;
- }
-
-
- File dirFile = new File(PATH);
- if (!dirFile.exists()) {
- dirFile.mkdir();
- }
-
-
- FileOutputStream fOut = null;
- try {
- fOut = new FileOutputStream(PATH + bitName + ".png");
- mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
- fOut.flush();
- fOut.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
-
-
- public static Bitmap stretch(Bitmap mBitmap, int newW, int newh) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
- colors = stretch(colors, w, h, newW, newh);
- return createBitmap(colors, newW, newh);
- }
-
-
-
-
- public static Bitmap imgToGray(Bitmap mBitmap) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
- colors = imgToGray(colors, w, h);
- return createBitmap(colors, w, h);
- }
-
-
-
-
- public static Bitmap binarization(Bitmap mBitmap, int methodCode) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
-
- switch (methodCode) {
- case 0:
- colors = binarization(colors, w, h);
- break;
- case 1:
- colors = binarization2(colors, w, h);
- break;
- default:
- throw new IllegalArgumentException("请选择正确的二值方法,现有0或1。");
- }
- return createBitmap(colors, w, h);
- }
-
-
-
-
-
-
- public static Bitmap filling(Bitmap mBitmap) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
- colors = filling(colors, w, h);
- return createBitmap(colors, w, h);
- }
-
-
-
-
-
-
- public static Bitmap dilation(Bitmap mBitmap) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
- colors = dilation(colors, w, h);
- return createBitmap(colors, w, h);
- }
-
-
-
-
-
-
- public static Bitmap erosion(Bitmap mBitmap) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
- colors = erosion(colors, w, h);
- return createBitmap(colors, w, h);
- }
-
-
-
-
-
-
- public static Bitmap erosion(Bitmap mBitmap, int iterations) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
- while (iterations-- > 0) {
- colors = erosion(colors, w, h);
- }
- return createBitmap(colors, w, h);
- }
-
-
-
-
- public static Bitmap thinning(Bitmap mBitmap, int methodCode) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
-
- switch (methodCode) {
- case 0:
- colors = thinning(colors, w, h);
- break;
- case 1:
- colors = thinning2(colors, w, h);
- break;
- default:
- throw new IllegalArgumentException("请选择正确的细化方法,现有0或1。");
- }
- return createBitmap(colors, w, h);
- }
-
-
-
-
- public static void split(Bitmap mBitmap) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
- split(colors, w, h);
- }
-
-
-
-
- public static Bitmap getSplitBmp(int index) {
-
- int[] colors = getSplitImg(index);
- if (null == colors) {
- throw new IllegalStateException("请确认已执行了分割图像,并且索引未越界。");
- }
-
- return createBitmap(colors, getSplitImgW(index), getSplitImgH(index));
- }
-
-
-
-
- public static Bitmap[] getSplitBmps() {
- int num = getSplitNum();
- Bitmap bitmap[] = new Bitmap[num];
- for (int i = 0; i < num; i++) {
- bitmap[i] = getSplitBmp(i);
- }
- return bitmap;
- }
-
-
-
-
-
-
- public static char analyseImg(Bitmap mBitmap) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
- return analyseImg(colors, w, h);
- }
-
-
-
-
- public static Bitmap binaryCid(Bitmap mBitmap) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- int[] colors = getColors(mBitmap, w, h);
- colors = binaryCid(colors, w, h);
- return createBitmap(colors, w, h);
- }
-
-
-
-
- public static int[] getColors(Bitmap mBitmap, int w, int h) {
- int[] pix = new int[w * h];
- mBitmap.getPixels(pix, 0, w, 0, 0, w, h);
- return pix;
- }
-
-
-
-
- public static Bitmap createBitmap(int[] colors, int w, int h) {
- Bitmap img = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
- img.setPixels(colors, 0, w, 0, 0, w, h);
- return img;
- }
-
- public static String getTime() {
- return String.valueOf(System.currentTimeMillis());
- }
-
- public void sayHello(String msg) {
- Log.e("C调用Java", msg);
- }
-
- public static native int[] stretch(int[] buf, int srcW, int srcH, int dstW,
- int dstH);
-
- public static native int[] imgToGray(int[] buf, int w, int h);
-
- public static native int[] binarization(int[] buf, int w, int h);
-
- public static native int[] binarization2(int[] buf, int w, int h);
-
- public static native int[] filling(int[] buf, int w, int h);
-
- public static native int[] dilation(int[] buf, int w, int h);
-
- public static native int[] erosion(int[] buf, int w, int h);
-
- public static native int[] thinning(int[] buf, int w, int h);
-
- public static native int[] thinning2(int[] buf, int w, int h);
-
- public static native void split(int[] buf, int w, int h);
-
- public static native int getSplitNum();
-
- public static native int[] getSplitImg(int index);
-
- public static native int getSplitImgW(int index);
-
- public static native int getSplitImgH(int index);
-
- public static native char analyseImg(int[] buf, int w, int h);
-
- public static native int[] locateCid(int[] buf, int w, int h);
-
- public static native int[] binaryCid(int[] buf, int w, int h);
-
- }
2
)ImageActivity.java
Activity类,界面&处理逻辑。
- public class ImageActivity extends Activity implements OnClickListener,
- OnItemClickListener {
-
- private Button[] buttons;
- private int enabledBtnIndex;
-
- private ImageView imageView;
- private Bitmap cardBitmap;
-
- private GridView gridView;
- private Bitmap splitBmps[];
-
- private boolean isSave = false;
- private int erosionCount = 1;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
- }
-
-
-
-
- @Override
- public void onClick(View v) {
-
- buttons[enabledBtnIndex].setEnabled(false);
- switch (v.getId()) {
- case R.id.takeBtn: {
-
-
- final String[] items = { "330122198102212239",
- "15210319861215033X", "370305196708031216",
- "210203196809236015", "411224196902244239" };
-
-
-
-
-
- final int[] ids = { R.drawable.idcard_1, R.drawable.idcard_2,
- R.drawable.idcard_3, R.drawable.idcard_4,
- R.drawable.idcard_5 };
-
- new AlertDialog.Builder(this)
- .setTitle("选择内置身份证号码")
- .setItems(items, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
-
- InputStream is = getResources().openRawResource(
- ids[which]);
- BitmapDrawable bmpDraw = new BitmapDrawable(is);
-
- cardBitmap = correctBitmap(bmpDraw.getBitmap());
-
- imageView.setImageBitmap(cardBitmap);
-
- imageView.setVisibility(View.VISIBLE);
-
- gridView.setVisibility(View.GONE);
-
- splitBmps = null;
-
- setNextBtnEnabled();
- }
- })
- .setPositiveButton("或拍照取像",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
-
- buttons[enabledBtnIndex].setEnabled(true);
-
- Intent intent = new Intent(
- getApplicationContext(),
- CameraActivity.class);
- startActivityForResult(intent, 1);
- }
- }).setCancelable(false).show();
- break;
- }
- case R.id.toGraybtn: {
-
- cardBitmap = JoinImage.imgToGray(cardBitmap);
- imageView.setImageBitmap(cardBitmap);
-
- if (isSave) {
- JoinImage.saveBitmap("1_gray", cardBitmap);
- }
-
- setNextBtnEnabled();
- break;
- }
- case R.id.toBinaBtn: {
-
- cardBitmap = JoinImage.binarization(cardBitmap, 0);
- imageView.setImageBitmap(cardBitmap);
-
- if (isSave) {
- JoinImage.saveBitmap("2_bina", cardBitmap);
- }
-
- setNextBtnEnabled();
- break;
- }
- case R.id.toFillBtn: {
-
- cardBitmap = JoinImage.filling(cardBitmap);
- imageView.setImageBitmap(cardBitmap);
-
- if (isSave) {
- JoinImage.saveBitmap("3_fill", cardBitmap);
- }
-
- setNextBtnEnabled();
- break;
- }
- case R.id.toDilaBtn: {
-
- cardBitmap = JoinImage.dilation(cardBitmap);
- imageView.setImageBitmap(cardBitmap);
-
- if (isSave) {
- JoinImage.saveBitmap("4_dila", cardBitmap);
- }
-
- setNextBtnEnabled();
- break;
- }
- case R.id.toErosBtn: {
-
-
- cardBitmap = JoinImage.erosion(cardBitmap, erosionCount);
- imageView.setImageBitmap(cardBitmap);
-
- if (isSave) {
- JoinImage.saveBitmap("5_eros", cardBitmap);
- }
-
- setNextBtnEnabled();
- break;
- }
- case R.id.toThinBtn: {
-
- cardBitmap = JoinImage.thinning(cardBitmap, 1);
- imageView.setImageBitmap(cardBitmap);
-
- if (isSave) {
- JoinImage.saveBitmap("6_thin", cardBitmap);
- }
-
- setNextBtnEnabled();
- break;
- }
- case R.id.toSplitBtn: {
-
- JoinImage.split(cardBitmap);
-
- splitBmps = JoinImage.getSplitBmps();
-
- gridView.setAdapter(new ImageAdapter(this, splitBmps));
-
- imageView.setVisibility(View.GONE);
-
- gridView.setVisibility(View.VISIBLE);
-
- cardBitmap = null;
-
- if (isSave && null != splitBmps) {
- for (int i = 0; i < splitBmps.length; i++) {
- JoinImage.saveBitmap("split_" + i, splitBmps[i]);
- }
- }
-
- setNextBtnEnabled();
- break;
- }
- }
- }
-
-
-
-
- private Bitmap correctBitmap(Bitmap mBitmap) {
- int w = mBitmap.getWidth(), h = mBitmap.getHeight();
- if (w >= 1500) {
- erosionCount = 2;
- int newW = 1000;
- int newH = h * newW / w;
-
- Bitmap newBitmap = JoinImage.stretch(mBitmap, newW, newH);
-
- if (isSave) {
- JoinImage.saveBitmap("0_stre", newBitmap);
- }
- return newBitmap;
- } else if (w >= 1000) {
- erosionCount = 2;
- return mBitmap;
- } else if (w <= 800) {
- erosionCount = 2;
- if (w <= 400) {
- Log.i("图像修正", "图像像素过低!");
- }
- int newW = 1000;
- int newH = h * newW / w;
-
- Bitmap newBitmap = JoinImage.stretch(mBitmap, newW, newH);
-
- if (isSave) {
- JoinImage.saveBitmap("0_stre", newBitmap);
- }
- return newBitmap;
- } else {
- erosionCount = 1;
- return mBitmap;
- }
- }
-
-
-
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- Toast.makeText(this, "->" + JoinImage.analyseImg(splitBmps[position]),
- Toast.LENGTH_SHORT).show();
- }
-
- ......
-
- }
3
)截图
![二值化]()
![分割]()
4
)后记
不足之处,多多担待^^。
附件:http://down.51cto.com/data/2360515
本文转自winorlose2000 51CTO博客,原文链接:http://blog.51cto.com/vaero/856750,如需转载请自行联系原作者