1代码 MainActivity
package com.example.chen.myapplication;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imageView;
private TextView textView;
private Button button1,button2,button3,button4;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
//连接数据库
private String url="jdbc:mysql://192.168.65.99:3306/test";
private String user="rootes";
private String password="123456";
PreparedStatement statement=null;
Statement stat=null;
Connection conn=null;
private ImageView.ScaleType scaleType=null;
private Bitmap bitmap=null;
private Bitmap nowBitmap=null;
//然后再每个数据内查找指定r,g,b,出现的次数(0-255)出现的次数
ArrayList rList=new ArrayList();
ArrayList gList=new ArrayList();
ArrayList bList=new ArrayList();
//用于存储各个数据对应的值
LinkedHashMap rMap=new LinkedHashMap();
LinkedHashMap gMap=new LinkedHashMap();
LinkedHashMap bMap=new LinkedHashMap();
//用于返回进行处理后的数据,存储的对应的键和值
LinkedHashMap dealRMap=new LinkedHashMap();
LinkedHashMap dealGMap=new LinkedHashMap();
LinkedHashMap dealBMap=new LinkedHashMap();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView);
System.out.println("imageView"+imageView.getHeight()+"宽"+imageView.getWidth());
scaleType=imageView.getScaleType();
textView=(TextView)findViewById(R.id.textView);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button4=(Button)findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
radioButton1=(RadioButton)findViewById(R.id.radioButton1);
radioButton2=(RadioButton)findViewById(R.id.radioButton2);
radioButton3=(RadioButton)findViewById(R.id.radioButton3);
radioButton1.setOnClickListener(this);
radioButton2.setOnClickListener(this);
radioButton3.setOnClickListener(this);
//为按钮设置一个标记
button1.setTag(4);
button2.setTag(5);
button3.setTag(6);
button4.setTag(7);
radioButton1.setTag(1);
radioButton2.setTag(2);
radioButton3.setTag(3);
}
//监听事件
@Override
public void onClick(View v) {
Integer id=(Integer)v.getTag();
if(id==4){ //处理打开文件
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}else if(id==5) {
System.out.println("拍照");
Toast toast=Toast.makeText(getApplicationContext(),"该功能等待更新",
Toast.LENGTH_SHORT);
toast.show();
}else if(id==6){
System.out.println("取像");
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://192.168.65.99/test","rootes","123456");
System.out.println("连接数据库成功");
} catch (ClassNotFoundException e) {
System.out.println("连接数据库失败");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
//获取imageView的大小
int width=bitmap.getWidth();
int height=bitmap.getHeight();
System.out.println("图像的宽和高为:"+width+","+height);
//1,传递Bitmap然后获取list存储的RGB值
getRGBList(bitmap);
//2.把list的各个RGB进行处理后返回对应的键值对
rMap=getRGBMap(rList);
gMap=getRGBMap(gList);
bMap=getRGBMap(bList);
//3,通过处理键值对的数据,返回需要的sql字段的值
int[] rgbCount=new int[3];
rgbCount[0]=getDealRGB(rMap);
System.out.println("查询的图像的像素分布是"+rgbCount);
//4,拿sql字段的值与标准的数据库进行比较,判断得出识别结果.
String typeName=getSQLRGB(rgbCount);
//处理Bitmap然后形成带有水印的识别结果
Bitmap dealbt=dealBitmap(bitmap);
//获取图像对应的RGB值.返回sql操作的语句
// String sqls=GetRGB(bitmap);;
//显示新的Bitmap图片
imageView.setImageBitmap(dealbt);
}else if(id==7) {//保存图片
Bitmap bitmap=loadBitmapFromView(imageView);
InputPhoto(bitmap);
Toast toast=Toast.makeText(getApplicationContext(),"保存文件成功",
Toast.LENGTH_SHORT);
toast.show();
}else if(id==1){
textView.setText("中国在宋朝(公元960年)就有在上等绿茶中加入龙脑香");
System.out.println("价格");
}else if(id==2){
textView.setText("据史料记载清咸丰年间(1851-1861年)");
System.out.println("营养");
}else if(id==3){
textView.setText("花茶(Scented tea),又名香片");
System.out.println("品种");
}
}
//处理打开相机后的图片返回哦
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode, resultCode, data);
System.out.println("打开了图片");
if(resultCode==RESULT_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
//缩放图片,使得和imageView的大小一样
nowBitmap=Bitmap.createScaledBitmap(bitmap,
497,201,true);
imageView.setImageBitmap(nowBitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
//1.获取bitmap中的RGBList
public void getRGBList(Bitmap bitmap1){
int w=bitmap1.getWidth();
int h=bitmap1.getHeight();
int[][] gray=new int[w][h];
for(int i=1;i>16;
int g=(int)(pixel&0xff00)>>8;
int b=(int)(pixel&0xff);
if(r>=230&&g>=230&&b>=230){
}else{
rList.add(r);
gList.add(g);
bList.add(b);
}
}
}
}
//2.通过RGBList获取RGBMap
public LinkedHashMap getRGBMap(ArrayList dealList){
LinkedHashMap dealMap=new LinkedHashMap();
for(int i=0;i mapSet=dealMap.keySet();//返回所有的键的集合
//System.out.println("长度大小"+mapSet.size());
if(mapSet.contains(key)){//如果包含该元素,取出加1
int value=dealMap.get(key);
value++;
dealMap.put(key,value);
}else{//如果不存在该元素,创建一个新的元素
dealMap.put(key,1);
}
}
//返回的key是乱序的,需要对该key进行重新排序
LinkedHashMap dealMaps=new LinkedHashMap();
Set keys=dealMap.keySet();
ArrayList list=new ArrayList(keys);
Collections.sort(list);//对list进行排序
for(int i=0;i it=dealMaps.keySet().iterator();it.hasNext();){
int key=it.next();
System.out.println("key"+key+",value="+dealMaps.get(key));
}*/
System.out.println("list的长度为:"+dealList.size());
System.out.println("map的长度为:"+dealMaps.size());
return dealMaps;
}
//3.通过处理map获取rgb中对应的字段值
public int getDealRGB(LinkedHashMap rgbMap) {
int flag=0;
int center=0;
int count=0;
Collection c=rgbMap.values();
Object[] obj=c.toArray();
Arrays.sort(obj);//对获取的值内容进行由小到大排序
center=((int)obj[0]+(int)obj[obj.length-1])/2;
Set keys=rgbMap.keySet();
Iterator iterator=keys.iterator();
while(iterator.hasNext()){
int key=iterator.next();
int value=rgbMap.get(key);
if(value>=center){
count=count+(value-center);
}
}
return count;
}
//4.通过给RGB的结果与字段中的RGB值进行匹配
public String getSQLRGB(int[] rgbCount){
String typeName="";
return typeName;
}
//处理bitmap
public Bitmap dealBitmap(Bitmap bitmap) {
//获取原始图片与水印图片的宽和高
int mBitmapWidth = bitmap.getWidth();
int mBitmapHeight = bitmap.getHeight();
Bitmap mNewBitmap = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight,
Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(mNewBitmap);
//绘制原始图片
mCanvas.drawBitmap(bitmap,0,0,null);
//添加文字
Paint mPaint = new Paint();
String string = "花茶";
mPaint.setColor(Color.RED);
mPaint.setTextSize(40);
for(int i=0;i<6;i++) {
mCanvas.drawText(string,mBitmapWidth/2,mBitmapHeight/2, mPaint);
mCanvas.save(Canvas.ALL_SAVE_FLAG);
mCanvas.restore();
}
return mNewBitmap;
}
///获取图片的RGB值
private String GetRGB(Bitmap bitmap) {
int w=bitmap.getWidth();
int h=bitmap.getHeight();
StringBuffer sql=new StringBuffer();
sql.append("insert into tb_rgb(x,y,r,g,b)values ");
for(int i=1;i>16)&0xff;
int g =( argb>>8)&0xff;
int b =argb&0xff;
int a =(argb>>24)&0xff;
int rgb =(a<<24)|(r<<16)|(g<<8)|b;
if((r!=255)&&(g!=255)&&(b!=255))
sql.append("("+j+","+i+","+r+","+j+","+b+"),");
// System.out.println("R="+r+",G="+g+",B="+b);
bitmap.setPixel(j,i,Color.RED);
}
}
return sql.toString();
}
//获取imageView中的图片
private Bitmap loadBitmapFromView(ImageView imageView) {
imageView.setDrawingCacheEnabled(true);
imageView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
imageView.setDrawingCacheBackgroundColor(Color.WHITE);
int w = imageView.getWidth();
int h = imageView.getHeight();
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawColor(Color.WHITE);
//如果不设置canvas的画布为白色,则生成透明
imageView.layout(0, 0, w, h);
imageView.draw(canvas);
return bmp;
}
//保存图片文件
private void InputPhoto(Bitmap bitmap) {
FileOutputStream fos;
String imagePath="";
try {
boolean isHasSDCard = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (isHasSDCard) {
File sdRoot = Environment.getExternalStorageDirectory();
File file = new File(sdRoot, Calendar.getInstance().
getTimeInMillis() + ".png");
System.out.println("文件路径"+file.toString());
fos = new FileOutputStream(file);
imagePath = file.getAbsolutePath();
}else {
throw new Exception("创建文件夹失败了");
}
bitmap.compress(Bitmap.CompressFormat.PNG,90,fos);
fos.flush();
fos.close();
}catch (Exception e) {
System.out.println("文件保存失败");
e.printStackTrace();
}
imageView.destroyDrawingCache();
System.out.println("保存文件成功");
}
}
2.activity.xml
3 AndroidMainActivity.xml
思路是:(1)获取整个图片的所有rgb数量存储在list中
(2)统计每个list中R,g,b,各个在每个list中存储值对应的数量,存储到map中,存储中key的大小是乱序的,需要重新排序,
(3)把map中的特征图进行处理,提取特征数据存储数据库,形成了该图像的特征数据.
(4)识别图像与数据库中的数据进行比对,找到对应的数据.