Android调用摄像头识别图片的形状和颜色
怎么调用里面的方法识别图片的各种颜色的图形有多少个,
下面是历程

    private int red_max=150,green_max=150,blues_max=150;               //输入颜色的阈值    private int red_min=200,green_min=170,blues_min=180;// // 储存图片左边像素坐标ArrayList<Coordinates> rlistl = new ArrayList<Coordinates>();ArrayList<Coordinates> glistl = new ArrayList<Coordinates>();ArrayList<Coordinates> blistl = new ArrayList<Coordinates>();// // 储存图片右边像素坐标ArrayList<Coordinates> rlistr = new ArrayList<Coordinates>();ArrayList<Coordinates> glistr = new ArrayList<Coordinates>();ArrayList<Coordinates> blistr = new ArrayList<Coordinates>();private int RGB_num = 0;private Bitmap convertToBlack(Bitmap bip, int index) {// 像素处理背景变为黑色,红绿蓝不变RGB_num = index;int width = bip.getWidth();int height = bip.getHeight();int[] pixels = new int[width * height];bip.getPixels(pixels, 0, width, 0, 0, width, height);int[] pl = new int[bip.getWidth() * bip.getHeight()];for (int y = 0; y < height; y++) {int offset = y * width;for (int x = 0; x < width; x++) {int pixel = pixels[offset + x];int r = (pixel >> 16) & 0xff;int g = (pixel >> 8) & 0xff;int b = pixel & 0xff;if (index==1&& r >red_min && g < green_max && b < blues_max)     // 红色pl[offset + x] = 0xFFFF0000;else if(index==2&& r <red_max && g > green_min && b < blues_max)  // 绿色pl[offset + x] = 0xFF00FF00;else if (index==3&&r < red_max && g <green_max && b > blues_min)  // 蓝色pl[offset + x] = 0xFF0000FF;elsepl[offset + x] = 0xff000000;// 黑色}}Bitmap result = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);result.setPixels(pl, 0, width, 0, 0, width, height);return result;}// 沉睡public void yanchi(int time) {try {Thread.sleep(time);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}ArrayList<Coordinates> list = new ArrayList<Coordinates>();ArrayList<Coordinates> list_above = new ArrayList<Coordinates>();ArrayList<Coordinates> list_among = new ArrayList<Coordinates>();ArrayList<Coordinates> list_below = new ArrayList<Coordinates>();private int cnum = 2;private boolean flag_go = true;private void shape_recognition(Bitmap bitmap_new){list.clear();//清空列表list_above.clear();list_among.clear();list_below.clear();int width = bitmap_new.getWidth();//得到int height = bitmap_new.getHeight();System.out.println("图片总高度"+height);System.out.println("图片总宽度"+width);int[] pixels = new int[width * height];bitmap_new.getPixels(pixels, 0, width, 0, 0, width, height);for (int y = 0; y< height; y++) {for (int x = 0; x < width; x++) {int pixel = pixels[y*width + x];int r = (pixel >> 16) & 0xff;int g = (pixel >> 8) & 0xff;int b = pixel & 0xff;if ((r>200) && (g<100) && (b<100)) { // 红色list.add(new Coordinates(x, y));}if ((r<100) && (g>200) && (b<100)) { // 绿色list.add(new Coordinates(x, y));}if ((r<100) && (g<100) && (b>200)) {// 蓝色list.add(new Coordinates(x, y));}}}int A_h = 0;int B_h = 0;int AB_h = 0;if((list.size() - cnum )>0){A_h = list.get(cnum).getY();B_h = list.get(list.size()-cnum).getY();if((list.get(list.size()-2).getY() - list.get(2).getY())>0){AB_h = list.get(list.size()-2).getY() - list.get(2).getY();System.out.println("形状总高度"+AB_h);flag_go = true;} else {flag_go = false;Looper.prepare();Toast.makeText(MainActivity.this, "抠图失败", Toast.LENGTH_LONG).show();Looper.loop();}} else {Looper.prepare();Toast.makeText(MainActivity.this, "参数设置冲突", Toast.LENGTH_LONG).show();Looper.loop();}int above_A_x = 0;int above_B_x = 0;int above_AB_x = 0;if(flag_go == true){for (int x = 0; x < width; x++) {int pixel = pixels[(AB_h/3)*width + x];int r = (pixel >> 16) & 0xff;int g = (pixel >> 8) & 0xff;int b = pixel & 0xff;if ((r>200) && (g<100) && (b<100)) { // 红色list_above.add(new Coordinates(x, A_h));}if ((r<100) && (g>200) && (b<100)) { // 绿色list_above.add(new Coordinates(x, A_h));}if ((r<100) && (g<100) && (b>200)) {// 蓝色list_above.add(new Coordinates(x, A_h));}}if((list_above.size()-cnum) >0){above_A_x = list_above.get(cnum).getX();above_B_x = list_above.get(list_above.size()-cnum).getX();if((above_B_x - above_A_x)>0){above_AB_x = list_above.get(list_above.size()-cnum).getX() - list_above.get(cnum).getX();System.out.println("上边长度"+above_AB_x);flag_go = true;} else {Looper.prepare();                                                                                            //调用looper函数,循环直到返回nullToast.makeText(MainActivity.this, "获取基准线一失败", Toast.LENGTH_LONG).show();Looper.loop();flag_go = false;}} else {Looper.prepare();Toast.makeText(MainActivity.this, "获取基准线一参数有误", Toast.LENGTH_LONG).show();Looper.loop();flag_go = false;}}int among_A_x = 0;int among_B_x = 0;int among_AB_x = 0;if( flag_go = true){for (int x = 0; x < width; x++) {int pixel = pixels[(AB_h/2)*width + x];int r = (pixel >> 16) & 0xff;int g = (pixel >> 8) & 0xff;int b = pixel & 0xff;if ((r>200) && (g<100) && (b<100)) { // 红色list_among.add(new Coordinates(x, A_h));}if ((r<100) && (g>200) && (b<100)) { // 绿色list_among.add(new Coordinates(x, A_h));}if ((r<100) && (g<100) && (b>200)) {// 蓝色list_among.add(new Coordinates(x, A_h));}}if((list_among.size()-cnum)>0){among_A_x = list_among.get(cnum).getX();among_B_x = list_among.get(list_among.size()-cnum).getX();if((among_B_x - among_A_x)>0){among_AB_x = list_among.get(list_among.size()-cnum).getX() - list_among.get(cnum).getX();System.out.println("中间长度"+among_AB_x);flag_go = true;} else {Looper.prepare();Toast.makeText(MainActivity.this, "获取基准线二失败", Toast.LENGTH_LONG).show();Looper.loop();flag_go = false;}} else {Looper.prepare();Toast.makeText(MainActivity.this, "获取基准线二参数有误", Toast.LENGTH_LONG).show();Looper.loop();flag_go = false;}}int below_A_x = 0;int below_B_x = 0;int below_AB_x = 0;if(flag_go == true){for (int x = 0; x < width; x++) {int pixel = pixels[((AB_h*2)/3)*width + x];int r = (pixel >> 16) & 0xff;int g = (pixel >> 8) & 0xff;int b = pixel & 0xff;if ((r>200) && (g<100) && (b<100)) { // 红色list_below.add(new Coordinates(x, A_h));}if ((r<100) && (g>200) && (b<100)) { // 绿色list_below.add(new Coordinates(x, A_h));}if ((r<100) && (g<100) && (b>200)) {// 蓝色list_below.add(new Coordinates(x, A_h));}}if((list_below.size()-cnum)>0){below_A_x = list_below.get(cnum).getX();below_B_x = list_below.get(list_below.size()-cnum).getX();if((below_B_x - below_A_x)>0){below_AB_x = list_below.get(list_below.size()-cnum).getX() - list_below.get(cnum).getX();System.out.println("下边长度"+below_AB_x);flag_go = true;} else {Looper.prepare();Toast.makeText(MainActivity.this, "获取基准线三失败", Toast.LENGTH_LONG).show();Looper.loop();flag_go = false;}} else {Looper.prepare();Toast.makeText(MainActivity.this, "获取基准线三失败", Toast.LENGTH_LONG).show();Looper.loop();flag_go = false;}}if(((among_AB_x-above_AB_x)<50) && ((among_AB_x - below_AB_x)<50))  //矩形{switch(RGB_num){case 1:phHandler.sendEmptyMessage(70);break;case 2:phHandler.sendEmptyMessage(71);break;case 3:phHandler.sendEmptyMessage(72);break;}}elseif((above_AB_x) < (among_AB_x) && (among_AB_x) < (below_AB_x))  //三角形{switch(RGB_num){case 1:phHandler.sendEmptyMessage(60);break;case 2:phHandler.sendEmptyMessage(61);break;case 3:phHandler.sendEmptyMessage(62);break;}}elseif((above_AB_x) < (among_AB_x) && (among_AB_x) > (below_AB_x))  //圆形{switch(RGB_num){case 1:phHandler.sendEmptyMessage(50);break;case 2:phHandler.sendEmptyMessage(51);break;case 3:phHandler.sendEmptyMessage(52);break;}} else {phHandler.sendEmptyMessage(80);}}}

更多相关文章

  1. Androd之在图片右上角显示红色圆圈里面数字提醒
  2. Android 超级水平仪 金属红色仪表风格发布
  3. android4.0系统点击后颜色-浅蓝色
  4. Android导入项目时出现红色感叹号
  5. Android 4.x 去除输入框的蓝色边框
  6. HTML5 蓝色 3D 钻石旋转动画
  7. 里面的获得蓝色边框
  8. phpmyadmin表前出现红色感叹号
  9. hibernate(*.hbm.xml)中新添加的字段被标记为红色(找不到)的解决方法

随机推荐

  1. Android 全局异常处理
  2. android ViewParent requestDisallowInte
  3. android 事件的获取
  4. android选择时间攻略
  5. Android Studio 之 启动和停止服务
  6. Android 四大组件之 Activity
  7. Android 以太网调用流程
  8. Android intent跳转工具类
  9. Android handler使用说明
  10. Android监视器概述