安卓实现图像识别,半成品(未完成)

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<Integer> rList=new ArrayList<Integer>();
    ArrayList<Integer> gList=new ArrayList<Integer>();
    ArrayList<Integer> bList=new ArrayList<Integer>();
    //用于存储各个数据对应的值
    LinkedHashMap<Integer,Integer> rMap=new LinkedHashMap<Integer,Integer>();
    LinkedHashMap<Integer,Integer> gMap=new LinkedHashMap<Integer,Integer>();
    LinkedHashMap<Integer,Integer> bMap=new LinkedHashMap<Integer,Integer>();
    //用于返回进行处理后的数据,存储的对应的键和值
    LinkedHashMap<Integer,Integer> dealRMap=new LinkedHashMap<Integer,Integer>();
    LinkedHashMap<Integer,Integer> dealGMap=new LinkedHashMap<Integer,Integer>();
    LinkedHashMap<Integer,Integer> dealBMap=new LinkedHashMap<Integer,Integer>();


    @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<h;i++){//i表示的位置是y
            for(int j=1;j<w;j++){
                int pixel=bitmap1.getPixel(j,i);
                int r=(int)(pixel&0xff0000)>>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<Integer,Integer> getRGBMap(ArrayList<Integer> dealList){
        LinkedHashMap<Integer,Integer> dealMap=new LinkedHashMap<Integer,Integer>();
        for(int i=0;i<dealList.size();i++){
            int key=dealList.get(i);//返回指定的键
            //如果不在就创建一个,如果在指定键的值加1
            Set<Integer> 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<Integer,Integer> dealMaps=new LinkedHashMap<Integer,Integer>();
        Set<Integer> keys=dealMap.keySet();
        ArrayList<Integer> list=new  ArrayList<Integer>(keys);
        Collections.sort(list);//对list进行排序
        for(int i=0;i<list.size();i++){
            int value=dealMap.get(list.get(i));
            dealMaps.put(list.get(i),value);
        }
		/*for(Iterator<Integer> 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<Integer,Integer> rgbMap) {
        int flag=0;
        int center=0;
        int count=0;
        Collection<Integer> c=rgbMap.values();
        Object[] obj=c.toArray();
        Arrays.sort(obj);//对获取的值内容进行由小到大排序
        center=((int)obj[0]+(int)obj[obj.length-1])/2;
        Set<Integer> keys=rgbMap.keySet();
        Iterator<Integer> 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<h;i++){//i表示的位置是y
            for(int j=1;j<w;j++){
                int argb=bitmap.getPixel(j,i);
                int r =( argb>>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

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.chen.myapplication.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="16dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="20dp"
                    android:layout_marginTop="10dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="花茶助手"
                    android:textSize="20dp" />
            </LinearLayout>
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="185dp"
                >
                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    app:srcCompat="@mipmap/icon" />
            </FrameLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:gravity="center_horizontal"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="打开文件" />
                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="拍照" />
                <Button
                    android:id="@+id/button4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="保存图片" />
                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="取像" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="43dp"
                android:gravity="center_vertical"
                android:orientation="horizontal">
                <RadioGroup
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="50dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/radioButton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"

                        android:text="价格" />

                    <RadioButton
                        android:id="@+id/radioButton2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="营养" />

                    <RadioButton
                        android:id="@+id/radioButton3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="品种" />
                </RadioGroup>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="15dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/textview_border"
                    android:ellipsize="marquee"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:text="识别内容" />
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

3 AndroidMainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chen.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="com.miui.whetstone.permission.ACCESS_PROVIDER"/>
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="true"
        />

</manifest>

思路是:(1)获取整个图片的所有rgb数量存储在list中

(2)统计每个list中R,g,b,各个在每个list中存储值对应的数量,存储到map中,存储中key的大小是乱序的,需要重新排序,

(3)把map中的特征图进行处理,提取特征数据存储数据库,形成了该图像的特征数据.

(4)识别图像与数据库中的数据进行比对,找到对应的数据.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值