Google Vision Image recognition for Android

May 03, 2016 14:56


Originally published at Moishe Beshkin. You can comment here or there.

Recently Google announced new API for image recognition. It is one the most exiting solutions from Google and prices are quite affordable.
So, I decided to switch from CamFind API to Google’s, because the last one is much more efficient and faster. Camfind worked quite well, but it took time for upload of image and further recognition. So, the whole process took more than one minute. Google vision takes about 5 seconds at top.


1. Get Google Cloud API key.

You need to create a Key in Google Cloud Platform console.
Note: you need to create Server API Key and use it for all platforms. The following solution is Java-based and doesn’t work properly with Android Key.

2. Functions to get image from camera
Add the following functions to your activity in order to start and manipulate camera.

public void startCamera() { if (PermissionUtils.requestPermission(this, CAMERA_PERMISSIONS_REQUEST, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getCameraFile())); startActivityForResult(intent, CAMERA_IMAGE_REQUEST); } } public File getCameraFile() { File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); return new File(dir, FILE_NAME); }
After you take the picture, you need to get it in ActivityResult

@Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == GALLERY_IMAGE_REQUEST && resultCode == RESULT_OK && intent != null) { uploadImage(intent.getData()); } else if(requestCode == CAMERA_IMAGE_REQUEST && resultCode == RESULT_OK) { uploadImage(Uri.fromFile(getCameraFile())); } } public void uploadImage(Uri uri) { new CloudVisionTask(this, uri).execute(); }
3. The most interesting part - Image recognition task.

public class CloudVisionTask extends AsyncTask

issues and resolutions

Previous post Next post
Up