IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Android Discussion :

[OpenCv_Android] L'application se bloque en ouvrant la caméra


Sujet :

API standards et tierces Android

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2013
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juillet 2013
    Messages : 16
    Points : 21
    Points
    21
    Par défaut [OpenCv_Android] L'application se bloque en ouvrant la caméra
    Bonjour je suis en train de développer une application Android en intégrant la bibliothèque OpenCV 2.4.9.
    Le fonctionnement de cette application consiste à ouvrir l'appareil photo du téléphone pour prendre une photo afin d'exercer quelques modifications avant d'enregistrer l'image sur la mémoire du téléphone .
    Mais le problème que l'application se bloque directement dès que je l’exécute sur mon téléphone , malgré qu'aucun erreur syntaxique est découvrit par l'IDE (eclipse).

    Pouvez vous m'aidez ?
    Merci d'avance.

    Joint à ce sujet vous trouvez la totalité de mon code.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    package tn.rnu.ensi.ReelTimeWatermark;
     
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
     
    import org.opencv.android.BaseLoaderCallback;
    import org.opencv.android.CameraBridgeViewBase;
    import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
    import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
    import org.opencv.android.LoaderCallbackInterface;
    import org.opencv.android.NativeCameraView;
    import org.opencv.android.OpenCVLoader;
    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.core.Point;
    import org.opencv.core.Rect;
    import org.opencv.core.Scalar;
    import org.opencv.highgui.Highgui;
    import org.opencv.imgproc.Imgproc;
     
    import android.annotation.SuppressLint;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.hardware.Camera;
    import android.hardware.Camera.CameraInfo;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Environment;
    import android.preference.PreferenceManager;
    import android.provider.MediaStore;
    import android.provider.MediaStore.Images;
    import android.support.v4.app.FragmentActivity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.Toast;
     
    public final class MainActivity extends FragmentActivity implements CvCameraViewListener2 {
     
    	// recuperer les preferences partagés 
    	SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    	//General Settings
    	private String  visibility= preferences.getString("preferences_visibility","Visible");
    	private String  type= preferences.getString("preferences_type", "text");
    	private String  textWatermark=preferences.getString("textWaterMark", "iTesLab-ENSI");
    	private String  imageWatermark=preferences.getString("imagePicker", "ii");
    	private Integer opacity=preferences.getInt ("opacityWatermark", 25);
     
    	// A tag for log output.
    	private static final String TAG = "MainActivity";
    	// A key for storing the index of the active camera.
    	private static final String STATE_CAMERA_INDEX = "cameraIndex";
    	// The index of the active camera.
    	private int mCameraIndex;
    	// Whether the active camera is front-facing.
    	// If so, the camera view should be mirrored.
    	private boolean mIsCameraFrontFacing;
    	// The number of cameras on the device.
    	private int mNumCameras;
    	// The camera view.
    	private CameraBridgeViewBase mCameraView;
    	// Whether the next camera frame should be saved as a photo.
    	private boolean mIsPhotoPending;
    	// A matrix that is used when saving photos.
    	private Mat mBgr;
    	// Whether an asynchronous menu action is in progress.
    	// If so, menu interaction should be disabled.
    	private boolean mIsMenuLocked;
    	// The OpenCV loader callback.
    	private BaseLoaderCallback mLoaderCallback =
    			new BaseLoaderCallback(this) {
    		@Override
    		public void onManagerConnected(final int status) {
    			switch (status) {
    			case LoaderCallbackInterface.SUCCESS:
    				Log.d(TAG, "OpenCV loaded successfully");
    				mCameraView.enableView();
    				mBgr = new Mat();
    				break;
    			default:
    				super.onManagerConnected(status);
    				break;
    			}
    		}
    	};
     
    	@SuppressLint("NewApi")
    	@Override
    	protected void onCreate(final Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
     
    		final Window window = getWindow();
    		window.addFlags(
    				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     
    		if (savedInstanceState != null) {
    			mCameraIndex = savedInstanceState.getInt(
    					STATE_CAMERA_INDEX, 0);
    		} else {
    			mCameraIndex = 0;
    		}
     
    		if (Build.VERSION.SDK_INT >=
    				Build.VERSION_CODES.GINGERBREAD) {
    			CameraInfo cameraInfo = new CameraInfo();
    			Camera.getCameraInfo(mCameraIndex, cameraInfo);
    			mIsCameraFrontFacing = 
    					(cameraInfo.facing ==
    					CameraInfo.CAMERA_FACING_FRONT);
    			mNumCameras = Camera.getNumberOfCameras();
    		} else { // pre-Gingerbread
    			// Assume there is only 1 camera and it is rear-facing.
    			mIsCameraFrontFacing = false;
    			mNumCameras = 1;
    		}
     
    		mCameraView = new NativeCameraView(this, mCameraIndex);
    		mCameraView.setCvCameraViewListener(this);
    		setContentView(mCameraView);
    	}
     
    	public void onSaveInstanceState(Bundle savedInstanceState) {
    		// Save the current camera index.
    		savedInstanceState.putInt(STATE_CAMERA_INDEX, mCameraIndex);
    		super.onSaveInstanceState(savedInstanceState);
    	}
     
    	@Override
    	public void onPause() {
    		if (mCameraView != null) {
    			mCameraView.disableView();
    		}
    		super.onPause();
    	}
     
    	@Override
    	public void onResume() {
    		super.onResume();
    		OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9,this, mLoaderCallback);
    		mIsMenuLocked = false;
    	}
     
    	@Override
    	public void onDestroy() {
    		super.onDestroy();
    		if (mCameraView != null) {
    			mCameraView.disableView();
    		}
    	}
     
    	@Override
    	public boolean onCreateOptionsMenu(final Menu menu) {
    		getMenuInflater().inflate(R.menu.action_bar, menu);
    		if (mNumCameras < 2) {
    			// Remove the option to switch cameras, since there is
    			// only 1.
    			menu.removeItem(R.id.change_camera);
    		}
    		return true;
    	}
     
    	@Override
    	public boolean onOptionsItemSelected(final MenuItem item) {
    		if (mIsMenuLocked) {
    			return true;
    		}
    		switch (item.getItemId()) {
    		case R.id.change_camera:
    			mIsMenuLocked = true;
    			mCameraIndex++;
    			if (mCameraIndex == mNumCameras) {
    				mCameraIndex = 0;
    			}
    			recreate();
    			return true;
     
    		case R.id.take_photo:
    			mIsMenuLocked = true;
    			mIsPhotoPending = true;
    			return true;
     
    		case R.id.change_settings:
    			mIsMenuLocked = true;
    			Intent settingsActivity = new Intent(MainActivity.this,SettingsActivity.class);
    			startActivity(settingsActivity);
    			return true;
     
    		case R.id.par_lot:
    			mIsMenuLocked = true;
    			Intent parLotActivity = new Intent(MainActivity.this,ParLotActivity.class);
    			startActivity(parLotActivity);
    			return true;
     
    		default:
    			return super.onOptionsItemSelected(item);
    		}
    	}
     
    	@Override
    	public void onCameraViewStarted(final int width,
    			final int height) {
    	}
     
    	@Override
    	public void onCameraViewStopped() {
    	}
     
    	@Override
    	public Mat onCameraFrame(final CvCameraViewFrame inputFrame) {
    		final Mat rgba = inputFrame.rgba();
     
    		if (mIsPhotoPending) {
    			mIsPhotoPending = false;
    			takePhoto(rgba);
    		}
     
    		if (mIsCameraFrontFacing) {
    			// Mirror (horizontally flip) the preview.
    			Core.flip(rgba, rgba, 1);
    		}
     
    		return rgba;
    	}
     
    	private void takePhoto(final Mat rgba) {
     
    		// Determine the path and metadata for the photo.
    		final long currentTimeMillis = System.currentTimeMillis();
    		final String appName = getString(R.string.app_name);
    		final String galleryPath =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    		final String albumPath = galleryPath + "/" + appName;
    		final String photoPath = albumPath + "/"+ currentTimeMillis + ".png";
    		final ContentValues values = new ContentValues();
    		values.put(MediaStore.MediaColumns.DATA, photoPath);
    		values.put(Images.Media.MIME_TYPE,LabActivity.PHOTO_MIME_TYPE);
    		values.put(Images.Media.TITLE, appName);
    		values.put(Images.Media.DESCRIPTION, appName);
    		values.put(Images.Media.DATE_TAKEN, currentTimeMillis);
     
    		// Ensure that the album directory exists.
    		File album = new File(albumPath);
    		if (!album.isDirectory() && !album.mkdirs()) {
    			Log.e(TAG, "Failed to create album directory at " +albumPath);
    			onTakePhotoFailed();
    			return;
    		}
     
    		// Try to create the photo.
    		Imgproc.cvtColor(rgba, mBgr, Imgproc.COLOR_RGBA2BGR, 3);
    		if ( visibility=="Visible") {
    			if (type=="text") {
    				visibleTextWatermark(mBgr);
    			}else {
    				visibleImageWatermark(mBgr);
    			}	
    		}else if (visibility=="Invisible") {
    			if (type=="text") {
    				invisibleTextWatermark(mBgr);
    			}else {
    				invisibleImageWatermark(mBgr);
    			}
    		} 
     
    		if (!Highgui.imwrite(photoPath, mBgr)) {
    			Log.e(TAG, "Failed to save photo to " + photoPath);
    			onTakePhotoFailed();
    		}
    		Log.d(TAG, "Photo saved successfully to " + photoPath);
     
    		// Try to insert the photo into the MediaStore.
    		Uri uri;
    		try {
    			uri = getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
    		} catch (final Exception e) {
    			Log.e(TAG, "Failed to insert photo into MediaStore");
    			e.printStackTrace();
    			File photo = new File(photoPath);
    			if (!photo.delete()) {
    				Log.e(TAG, "Failed to delete non-inserted photo");
    			}
    			onTakePhotoFailed();
    			return;
    		}
     
    		// Open the photo in LabActivity.
    		final Intent labActivity = new Intent(MainActivity.this, LabActivity.class);
    		labActivity.putExtra(LabActivity.EXTRA_PHOTO_URI, uri);
    		labActivity.putExtra(LabActivity.EXTRA_PHOTO_DATA_PATH,photoPath);
    		startActivity(labActivity);
    	}
     
    	private void onTakePhotoFailed() {
    		mIsMenuLocked = false;
    		final String errorMessage =getString(R.string.photo_error_message);
    		runOnUiThread(new Runnable() {
    			@Override
    			public void run() {
    				Toast.makeText(MainActivity.this, errorMessage,Toast.LENGTH_SHORT).show();
    			}
    		});
    	}
     
    	private void visibleImageWatermark(Mat img){
    		Mat watermarkImage= Highgui.imread(imageWatermark);
    		float alpha= opacity/100;
    		if (watermarkImage.rows()>img.rows()||watermarkImage.cols()>img.cols()) {
    			Imgproc.resize(watermarkImage, watermarkImage, img.size());
    		}
    		Rect rectWatermark=new Rect(0,0,watermarkImage.cols(),watermarkImage.rows());
    		Core.addWeighted(img.submat(rectWatermark), 1-alpha, watermarkImage,
    				alpha, 1, img.submat(rectWatermark));
    	}
     
    	private void visibleTextWatermark(Mat img){
     
    		float alpha= opacity/100;
    		Scalar  color=new Scalar(255);
    		Core.putText(img, textWatermark,
    				new Point(img.rows()/2,img.cols()/2),
    				Core.FONT_HERSHEY_COMPLEX, alpha, color);
    	}
     
    	private void invisibleImageWatermark(Mat img){
     
    		Mat watermarkImage= Highgui.imread(imageWatermark);
    		List<Mat> channelsOriginal= new ArrayList<Mat>(3); 
    		Core.split(img, channelsOriginal);
    		Mat mR=channelsOriginal.get(0);	mR.convertTo(mR, CvType.CV_64FC1);	Core.dft(mR,mR);
    		Mat mG=channelsOriginal.get(1);	mG.convertTo(mG, CvType.CV_64FC1);	Core.dft(mG,mG);
    		Mat mB=channelsOriginal.get(2);	mB.convertTo(mB, CvType.CV_64FC1);	Core.dft(mB, mB);
     
    		Imgproc.resize(watermarkImage, watermarkImage, img.size());
     
    		List<Mat> channelsWater= new ArrayList<Mat>(3); 
    		Core.split(watermarkImage, channelsWater);
    		Mat mR1=channelsWater.get(0);	mR1.convertTo(mR1, CvType.CV_64FC1);	Core.dft(mR1,mR1);
    		Mat mG1=channelsWater.get(1);	mG1.convertTo(mG1, CvType.CV_64FC1);	Core.dft(mG1,mG1);
    		Mat mB1=channelsWater.get(2);	mB1.convertTo(mB1, CvType.CV_64FC1);	Core.dft(mB1, mB1);
     
    		Scalar alpha=new Scalar(0.03);
    		Core.multiply(mR1, alpha, mR1);
    		Core.multiply(mG1, alpha, mG1);
    		Core.multiply(mB1, alpha, mB1);
     
    		Core.add(mR, mR1, mR); Core.idft(mR,mR); mR.convertTo(mR, CvType.CV_8UC1);
    		Core.add(mG, mG1, mG); Core.idft(mG,mG); mG.convertTo(mG, CvType.CV_8UC1);
    		Core.add(mB, mB1, mB); Core.idft(mB,mB); mB.convertTo(mB, CvType.CV_8UC1);
     
    		Core.merge(channelsOriginal, img);
    	}
     
    	private void invisibleTextWatermark(Mat img){
     
    	}
     
    }
    Fichiers attachés Fichiers attachés

Discussions similaires

  1. Réponses: 9
    Dernier message: 04/04/2011, 10h20
  2. mon application est bloquée, freeze
    Par ericaix13 dans le forum C#
    Réponses: 11
    Dernier message: 28/10/2010, 20h07
  3. L'application se bloque lors de l'execution
    Par charouel dans le forum Windows Forms
    Réponses: 5
    Dernier message: 22/10/2009, 18h31
  4. Regex bloque l'application
    Par osoumayaj dans le forum Langage
    Réponses: 11
    Dernier message: 20/03/2006, 17h44
  5. Application bloquée
    Par bilal_ini dans le forum Langage
    Réponses: 4
    Dernier message: 22/11/2005, 14h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo