1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
| package mon.projet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import mon.projet.MonProjet;
import com.googlecode.tesseract.android.TessBaseAPI;
import mon.projet.R;
import mon.projet.MonProjet.ButtonClickHandler;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
public class MonProjet extends Activity implements
SurfaceHolder.Callback {
byte code;
String Path = "/sdcard/PhotoEditor/Test.png" ;
Button b = null;
private TabHost monTabHost;
private Camera camera;
private SurfaceView surfaceCamera;
private Boolean isPreview;
private FileOutputStream stream;
public static final String PACKAGE_NAME = "com.datumdroid.android.ocr.simple";
public static final String DATA_PATH = Environment
.getExternalStorageDirectory().toString() + "/SimpleAndroidOCR/";
// You should have the trained data file in assets folder
// You can get them at:
// http://code.google.com/p/tesseract-ocr/downloads/list
public static final String lang = "eng";
private static final String TAG = "SimpleAndroidOCR.java";
protected Button _button;
// protected ImageView _image;
protected EditText _field;
protected String _path;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
@Override
public void onCreate(Bundle savedInstanceState) {
//////////////////////////////////////////////////
////////////////////////////////////////////
String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };
for (String path : paths) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
return;
} else {
Log.v(TAG, "Created directory " + path + " on sdcard");
}
}
}
if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
try {
AssetManager assetManager = getAssets();
InputStream in = assetManager.open("tessdata/eng.traineddata");
//GZIPInputStream gin = new GZIPInputStream(in);
OutputStream out = new FileOutputStream(DATA_PATH
+ "tessdata/eng.traineddata");
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
//while ((lenf = gin.read(buff)) > 0) {
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
//gin.close();
out.close();
Log.v(TAG, "Copied " + lang + " traineddata");
} catch (IOException e) {
Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
}
}
/////////////////////////////////////////
/////////////////////////////////
super.onCreate(savedInstanceState);
// On met l'application en plein écran et sans barre de titre
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
isPreview = false;
setContentView(R.layout.main);
monTabHost =(TabHost) findViewById(R.id.TabHost01);
monTabHost.setup();
monTabHost.addTab(monTabHost.newTabSpec("onglet_1").setIndicator("Choix").setContent(R.id.Onglet1));
monTabHost.addTab(monTabHost.newTabSpec("onglet_2").setIndicator("Résultat").setContent(R.id.Onglet2));
monTabHost.addTab(monTabHost.newTabSpec("onglet_3").setIndicator("Scan").setContent(R.id.Onglet3));
monTabHost.addTab(monTabHost.newTabSpec("onglet_4").setIndicator("OCR").setContent(R.id.Onglet4));
monTabHost.setOnTabChangedListener(
new TabHost.OnTabChangeListener (){
public void onTabChanged(String tabId){
//Toast.makeText(MonProjet.this, "Longlet avec lidentifiant : "+ tabId + " a été cliqué", Toast.LENGTH_SHORT).show();
}
});
////////////////////////////
///////////////////////////
// _image = (ImageView) findViewById(R.id.image);
_field = (EditText) findViewById(R.id.monEditText3);
_button = (Button) findViewById(R.id.button);
_button.setOnClickListener(new ButtonClickHandler());
_path = DATA_PATH + "/ocr.jpg";
////////////////////////
///////////////////////
((CheckBox)findViewById(R.id.CheckBox02)).setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
code = 0001;
}
});
((CheckBox)findViewById(R.id.CheckBox03)).setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
code = 0010;
}
});
((CheckBox)findViewById(R.id.CheckBox04)).setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
code = 0011;
}
});
((CheckBox)findViewById(R.id.CheckBox05)).setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
code = 0100;
}
});
((CheckBox)findViewById(R.id.CheckBox06)).setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
code = 0101;
}
});
((CheckBox)findViewById(R.id.CheckBox07)).setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
code = 0110;
}
});
((CheckBox)findViewById(R.id.CheckBox08)).setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
code = 0111;
}
});
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String result = null;
EditText texte1 = ((EditText)findViewById(R.id.monEditText1));
String chiffre1 = texte1.getText().toString();
EditText texte2 = ((EditText)findViewById(R.id.monEditText2));
String chiffre2 = texte2.getText().toString();
switch(code){
case 0000 :
break;
case 0001 :
float somme = Float.parseFloat(chiffre1) + Float.parseFloat(chiffre2);
result = String.valueOf(somme);
break;
case 0010 :
float difference = Float.parseFloat(chiffre1) - Float.parseFloat(chiffre2);
result = String.valueOf(difference);
break;
case 0011 :
float produit = Float.parseFloat(chiffre1) * Float.parseFloat(chiffre2);
result = String.valueOf(produit);
break;
case 0100 :
float quotient = Float.parseFloat(chiffre1) / Float.parseFloat(chiffre2);
result = String.valueOf(quotient);
break;
case 0101 :
double cosinus = Math.cos(Double.parseDouble(chiffre1)*Math.PI/180);
result = String.valueOf(cosinus);
break;
case 0111 :
double sinus = Math.sin(Double.parseDouble(chiffre1)*Math.PI/180);
result = String.valueOf(sinus);
break;
}
Toast.makeText(MonProjet.this, result, Toast.LENGTH_LONG).show();
}
});
// Si vous voulez gérer plusieurs caméra sur votre mobile / tablette
for (int nbCamera = 0; nbCamera < Camera.getNumberOfCameras(); nbCamera++) {
Camera.CameraInfo informationsCamera = new Camera.CameraInfo();
Camera.getCameraInfo(nbCamera, informationsCamera);
if (informationsCamera != null) {
switch (informationsCamera.facing) {
case Camera.CameraInfo.CAMERA_FACING_FRONT:
// Caméra facial
break;
case Camera.CameraInfo.CAMERA_FACING_BACK:
// Caméra arrière
break;
}
}
}
// On récupère notre surface pour le preview
surfaceCamera = (SurfaceView) findViewById(R.id.surfaceViewCamera);
// Quand on clique sur notre surface
surfaceCamera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// On prend une photo
if (camera != null) {
SavePicture();
}
}
});
// Méthode d'initialisation de la caméra
InitializeCamera();
}
public void afficheToast(String text)
{
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
Log.v(TAG, "Starting Camera app");
startCameraActivity();
}
}
protected void startCameraActivity() {
File file = new File(_path);
Uri outputFileUri = Uri.fromFile(file);
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "resultCode: " + resultCode);
if (resultCode == -1) {
onPhotoTaken();
} else {
Log.v(TAG, "User cancelled");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(MonProjet.PHOTO_TAKEN, _taken);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(TAG, "onRestoreInstanceState()");
if (savedInstanceState.getBoolean(MonProjet.PHOTO_TAKEN)) {
onPhotoTaken();
}
}
protected void onPhotoTaken() {
_taken = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(_path, options);
try {
ExifInterface exif = new ExifInterface(_path);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Log.v(TAG, "Orient: " + exifOrientation);
int rotate = 0;
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}
Log.v(TAG, "Rotation: " + rotate);
if (rotate != 0) {
// Getting width & height of the given image.
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);
// Rotating Bitmap
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
}
// Convert to ARGB_8888, required by tess
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
} catch (IOException e) {
Log.e(TAG, "Couldn't correct orientation: " + e.toString());
}
// _image.setImageBitmap( bitmap );
Log.v(TAG, "Before baseApi");
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.setDebug(true);
baseApi.init(DATA_PATH, lang);
baseApi.setImage(bitmap);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
// You now have the text in recognizedText var, you can do anything with it.
// We will display a stripped out trimmed alpha-numeric version of it (if lang is eng)
// so that garbage doesn't make it to the display.
Log.v(TAG, "OCRED TEXT: " + recognizedText);
if ( lang.equalsIgnoreCase("eng") ) {
recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
}
recognizedText = recognizedText.trim();
if ( recognizedText.length() != 0 ) {
_field.setText(_field.getText().toString().length() == 0 ? recognizedText : _field.getText() + " " + recognizedText);
_field.setSelection(_field.getText().toString().length());
}
// Cycle done.
}
public void InitializeCamera() {
// On attache nos retour du holder à notre activite
surfaceCamera.getHolder().addCallback(this);
// On spécifie le type du holder en mode SURFACE_TYPE_PUSH_BUFFERS
surfaceCamera.getHolder().setType(
SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// Retour sur l'application
@Override
public void onResume() {
super.onResume();
camera = Camera.open();
camera.setDisplayOrientation(90);
}
// Mise en pause de l'application
@Override
public void onPause() {
super.onPause();
if (camera != null) {
camera.release();
camera = null;
}
}
// Implémentation du surface holder
// Quand la surface change
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// Si le mode preview est lancé alors on le stop
if (isPreview) {
camera.stopPreview();
}
// On récupère les parametres de la camera
Camera.Parameters parameters = camera.getParameters();
// On change la taille
parameters.setPreviewSize(width, height);
// On applique nos nouveaux parametres
camera.setParameters(parameters);
try {
// On attache notre previsualisation de la camera au holder de la
// surface
camera.setPreviewDisplay(surfaceCamera.getHolder());
} catch (IOException e) {
}
// On lance la previeuw
camera.startPreview();
isPreview = true;
}
public void surfaceCreated(SurfaceHolder holder) {
// On prend le controle de la camera
if (camera == null)
camera = Camera.open();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// On arrête la camera et on rend la main
if (camera != null) {
camera.stopPreview();
isPreview = false;
camera.release();
}
}
// Callback pour la prise de photo
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
if (data != null) {
// Enregistrement de votre image
try {
if (stream != null) {
stream.write(data);
stream.flush();
stream.close();
}
} catch (Exception e) {
// TODO: handle exception
}
// On redémarre la prévisualisation
camera.startPreview();
}
}
};
private void SavePicture() {
try {
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH.mm.ss");
String fileName = "photo_" + timeStampFormat.format(new Date())
+ ".jpg";
// Metadata pour la photo
ContentValues values = new ContentValues();
values.put(Media.TITLE, fileName);
values.put(Media.DISPLAY_NAME, fileName);
values.put(Media.DESCRIPTION, "Image prise par FormationCamera");
values.put(Media.DATE_TAKEN, new Date().getTime());
values.put(Media.MIME_TYPE, "image/jpeg");
// Support de stockage
Uri taken = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
// Ouverture du flux pour la sauvegarde
stream = (FileOutputStream) getContentResolver().openOutputStream(
taken);
camera.takePicture(null, pictureCallback, pictureCallback);
} catch (Exception e) {
// TODO: handle exception
}
}
} |
Partager