1.说明:
最近公司碰到一个需求,需要能拍照,然后识别拍照的内容,根据拍照的东西,做一下智能化的自动处理,因此参考了百度,阿里,腾讯三家的sdk,最后发现还是百度好用,真香。。。(Ps:有了这个智能识图功能,以后鉴黄师可以失业了,我尝试拍了许多的生活物品,又去网上下载了一堆图片,识别正确率还是挺高的)
注意,我们这个demo需要先选你识别的类型,然后再去做具体的识别,当然最好用的还是通用识别模块
1.1源码地址·:https://github.com/wrs13634194612/BaiduImage 需要的自行下载
2.效果图:
2.1 百度控制台,需要自己去注册账号和appid
2.2 运行效果图 这是我的杯子 ,识别的效果图
3.appid管理类:appid apikey secretkey 需要自己去百度开放者平台注册
package com.example.administrator.testz.util;
/**
* Created by INvo
* on 2019-07-11.
*/
public class Constants {
public static final String APP_ID = "176132";
public static final String API_KEY = "tOjf1g16FzDNofLw6FM7";
public static final String SECRET_KEY = "myvEcz6jGGjXuSzKvY8FW0gAkknTqH";
public static final int ACTION_CHOOSE_IMAGE = 1;//访问本地图片的请求码
public static final int REQUEST_CODE_TAKE_PICTURE = 2;//访问相机的请求码
}
4.主界面:核心代码都在主界面里面,
package com.example.administrator.testz;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.administrator.testz.BuildConfig;
import com.example.administrator.testz.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import com.example.administrator.testz.util.BitmapToByte;
import com.example.administrator.testz.util.BitmapUtil;
import com.example.administrator.testz.util.Classify;
import com.example.administrator.testz.util.Constants;
import com.example.administrator.testz.util.DataUtil;
import com.example.administrator.testz.util.LineProgress;
import com.example.administrator.testz.util.PhotoProcess;
import com.example.administrator.testz.util.ToastUtil;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
public class MainActivity extends AppCompatActivity {
private MainModel mViewModel;
private Spinner spinnerCountry;
private ImageView mImage, mChooseLocalImage, mCapture;
private View line;
private String imagePath = null;
private Bitmap mBitmap = null;//待处理的位图,默认为空
private JSONObject json;
private static final int ERROR = 5;
private LineProgress lineHandler = new LineProgress();
private BitmapUtil mBu = new BitmapUtil();
private DataUtil mDu = new DataUtil();
private PhotoProcess photoProcess = new PhotoProcess();
private BitmapToByte bitmapToByte = new BitmapToByte();
private Classify classify = new Classify();
private String countries[] = {"通用物体识别","菜品识别","车型识别","logo商标识别"
,"动物识别","植物识别","图像主体检测","地标识别"
,"花卉识别","食材识别","红酒识别","货币识别"};
private int style = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
mImage = findViewById(R.id.initImage_5);
spinnerCountry = findViewById(R.id.spinnerCountry);
mImage.setImageResource(R.drawable.wallpaper);
line = findViewById(R.id.progress_line_5);
mChooseLocalImage = findViewById(R.id.local_image_5);
mChooseLocalImage.setOnClickListener(new View.OnClickListener() {
@Override