Sitemap

Alpr system

3 min readDec 26, 2019

--

I want to share some source code I have used in a alpr project.Click here to see the alpr project online.Of course,the source code wich is used in the project has been updated,not completely the same as the bellow one.

Source code

import android.app.Activity;

import android.content.ContentValues;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Build;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.support.v7.app.AppCompatActivity;

import android.text.format.DateFormat;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Spinner;

import android.widget.TextView;

import android.widget.Toast;

import com.openalpr.api.DefaultApi;

import com.openalpr.api.invoker.ApiException;

import com.openalpr.api.models.InlineResponse200;

import com.yalantis.ucrop.UCrop;

import com.yalantis.ucrop.UCropActivity;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Calendar;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private final DefaultApi api = new DefaultApi();

private static final String TAG = “MainActivity”;

Button reconize,select;

TextView result,plate;

ImageView showImage;

Spinner spinner;

String secretKey = “sk_960d6f4c62d1c20452c7613e”;

String url = “https://qnwww2.autoimg.cn/youchuang/g6/M0A/FA/29/autohomecar__wKgH3FkZCr-AFH7zAAFPFgtP6pc751.jpg?imageView2/2/w/752|watermark/2/text/TEkzMw0K5rG96L2m5LmL5a62/font/5b6u6L2v6ZuF6buR/fontsize/270/fill/d2hpdGU=/dissolve/100/gravity/SouthEast/dx/5/dy/5";

File cameraFile,cropFile;

private boolean isFromfile = false;

String country = “us”;

final Integer recognizeVehicle = 0;

final String state = “”;

final Integer returnImage = 1;

final Integer topn = 10;

final String prewarp = “”;

private String cropPath = “”;

String[] countrys ;

String[] countryCodes ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

reconize = (Button) findViewById(R.id.reconize);

select = (Button) findViewById(R.id.selectImage);

result = (TextView) findViewById(R.id.result);

showImage = (ImageView) findViewById(R.id.imageShow);

plate = (TextView) findViewById(R.id.plate);

spinner = (Spinner) findViewById(R.id.spinner);

// GlideUtils.setUrlImage(this,url,showImage);

countrys = getResources().getStringArray(R.array.countrys);

countryCodes = getResources().getStringArray(R.array.country_codes);

select.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String filename = DateFormat.format(“yyyyMMdd_hhmmss”, Calendar.getInstance(Locale.CHINA)) + “.jpg”;

cameraFile = new File(Environment.getExternalStorageDirectory() + “/Pictures/Camera/” + filename);

if(!cameraFile.exists()){

try {

cameraFile.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

}

Intent intent0 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (Build.VERSION.SDK_INT < 24) {

intent0 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent0.putExtra(“android.intent.extras.CAMERA_FACING”, 0);

intent0.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));

} else {

ContentValues contentValues = new ContentValues(1);

intent0.putExtra(“android.intent.extras.CAMERA_FACING”, 0);

contentValues.put(MediaStore.Images.Media.DATA, cameraFile.getAbsolutePath());

Uri uri = MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

intent0.putExtra(MediaStore.EXTRA_OUTPUT, uri);

}

isFromfile = true;

startActivityForResult(intent0, 200);

}

});

reconize.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

result.setText(“identifying…”);

plate.setText(“”);

try {

new Thread(){

@Override

public void run() {

super.run();

InlineResponse200 response = null;

try {

if(isFromfile){

response = api.recognizeFile(cropFile, secretKey, country, recognizeVehicle, state, returnImage, topn, prewarp);

}else {

response = api.recognizeUrl(url, secretKey, country, recognizeVehicle, state, returnImage, topn, prewarp);

}

} catch (ApiException e) {

e.printStackTrace();

}

Get Hugh dreaming’s stories in your inbox

Join Medium for free to get updates from this writer.

final InlineResponse200 finalResponse = response;

runOnUiThread(new Runnable() {

@Override

public void run() {

if(finalResponse == null){

Toast.makeText(MainActivity.this, “识别失败”, Toast.LENGTH_SHORT).show();

return;

}

Toast.makeText(MainActivity.this, “识别成功”, Toast.LENGTH_SHORT).show();

if(finalResponse.getResults()!=null && finalResponse.getResults().size()>=1)

plate.setText(“license plate number:”+finalResponse.getResults().get(0).getPlate());

result.setText(“result:”+finalResponse.toString());

}

});

}

}.start();

} catch (Exception e) {

e.printStackTrace();

}

}

});

ArrayAdapter<String> product_adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice, countrys);

spinner.setAdapter(product_adapter);

spinner.setSelection(12);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

country = countryCodes[position];

}

@Override

public void onNothingSelected(AdapterView<?> parent) {

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if(resultCode != RESULT_OK){

return;

}

switch (requestCode) {

case 200:

if (!cameraFile.exists()) {

return ;

}

Bitmap bitmap = null;

if (resultCode== Activity.RESULT_OK){

try {

FileInputStream fis = new FileInputStream(cameraFile);

bitmap = BitmapFactory.decodeStream(fis);

fis.close();

} catch (Exception e) {

e.printStackTrace();

}

if (bitmap!=null) {

showImage.setImageBitmap(bitmap);

}

cropPath = startUCrop(this,cameraFile.getPath(),UCrop.REQUEST_CROP,16,9);

}

break;

case UCrop.REQUEST_CROP:

final Uri resultUri = UCrop.getOutput(data);

showImage.setImageURI(resultUri);

cropFile = new File(cropPath);

break;

default:

break;

}

}

/**

* Start the cutting

* @param activity context

* @param sourceFilePath You need to crop the absolute path of the image

* @param requestCode such as:UCrop.REQUEST_CROP

* @param aspectRatioX Crop image aspect ratio

* @param aspectRatioY Crop image aspect ratio

* @return

*/

public static String startUCrop(Activity activity, String sourceFilePath,

int requestCode, float aspectRatioX, float aspectRatioY) {

Uri sourceUri = Uri.fromFile(new File(sourceFilePath));

File outDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

if (!outDir.exists()) {

outDir.mkdirs();

}

File outFile = new File(outDir, System.currentTimeMillis() + “.jpg”);

//Absolute path of cropped image

String cameraScalePath = outFile.getAbsolutePath();

Uri destinationUri = Uri.fromFile(outFile);

//Initialization, first parameter: image to be cropped;

Second parameter: crop the image

UCrop uCrop = UCrop.of(sourceUri, destinationUri);

//Initialize the UCrop configuration

UCrop.Options options = new UCrop.Options();

//Set an actionable gesture to crop the image

options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ROTATE, UCropActivity.ALL);

//Whether to hide the bottom container, shown by default

options.setHideBottomControls(true);

// //Set toolbar color

// options.setToolbarColor(ActivityCompat.getColor(activity, R.color.colorPrimary));

// //Set the status bar color

// options.setStatusBarColor(ActivityCompat.getColor(activity, R.color.colorPrimary));

//Can I adjust the clipping box

options.setFreeStyleCropEnabled(true);

//UCrop seting

uCrop.withOptions(options);

//Set the aspect ratio of cropped images, such as 16:9

uCrop.withAspectRatio(aspectRatioX, aspectRatioY);

//uCrop.useSourceImageAspectRatio();

//Skip clipping page

uCrop.start(activity, requestCode);

return cameraScalePath;

}

}

--

--

Hugh dreaming
Hugh dreaming

Written by Hugh dreaming

0 followers

I am the owner of dreaming machinery