2017-10-04 6 views
-2

J'utilise Android Camera2 API et voudrais prendre 15 photos en continu. Le problème est qu'il prend avec succès quelques photos mais se bloque après 6 ~ 8 images.Application Android bloque un certain temps et fonctionne bien un certain temps sans accident

Voici les journaux:

Legacy-CameraDevice-JNI: LegacyCameraDevice_nativeGetSurfaceId: Impossible de récupérer surface natif de la surface. E/AndroidRuntime: FATAL EXCEPTION: Thread-1095 Processus: com.example.grobomac.traindriver, PID: 11690 java.lang.IllegalArgumentException: Surface n'avait pas de natif valide Surface. à android.hardware.camera2.legacy.LegacyCameraDevice.nativeGetSurfaceId (natif Method) à android.hardware.camera2.legacy.LegacyCameraDevice.getSurfaceId (LegacyCameraDevice.java:658) à android.hardware.camera2.legacy .LegacyCameraDevice.containsSurfaceId (LegacyCameraDevice.java:678) à android.hardware.camera2.legacy.RequestThreadManager $ 2.onPictureTaken (RequestThreadManager.java:225) à android.hardware.Camera $ EventHandler.handleMessage (Camera.java: 1272) chez android.os.Han dler.dispatchMessage (Handler.java:111) à android.os.Looper.loop (Looper.java:207) à android.hardware.camera2.legacy.CameraDeviceUserShim $ CameraLooper.run (CameraDeviceUserShim.java:136) à java.lang.Thread.run (Thread.java:818) 10-04 14: 26: 59,566 11690-11690/com.example.grobomac.traindriver E/AndroidCameraApi: OnPause

Et le code I utilisation:

public void onImageAvailable(ImageReader reader) { 
        Image image = null; 
        try { 
         image = reader.acquireLatestImage(); 
         ByteBuffer buffer = image.getPlanes()[0].getBuffer(); 
         byte[] bytes = new byte[buffer.capacity()]; 
         buffer.get(bytes); 
         save(bytes); 
         mBitmapToSave1 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
         mBitmapToSave = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
         Bitmap scaled = Bitmap.createScaledBitmap(mBitmapToSave, width, height, true); 
         int w = scaled.getWidth(); 
         int h = scaled.getHeight(); 
         // Setting post rotate to 90 
         Matrix mtx = new Matrix(); 
         mtx.postRotate(-180); 
         // Rotating Bitmap 
         mBitmapToSave = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true); 
         // mBitmapToSave = Bitmap.createBitmap(width+rowPadding/pixelStride,height, Bitmap.Config.RGB_565); 
         // mBitmapToSave.copyPixelsToBuffer(buffer); 

         if (detector.isOperational() && mBitmapToSave != null) { 
          Frame frame = new Frame.Builder().setBitmap(mBitmapToSave).build(); 
          SparseArray<Face> faces = detector.detect(frame); 

          for (index = 0; index < faces.size(); ++index) { 
           Face face = faces.valueAt(index); 
          } 
          if (faces.size() == 0) { 
           MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.not); 
           mediaPlayer.start(); 
           //Toast.makeText(AndroidCamera2API.this, "Face Not detected Adjust Camera Properly", Toast.LENGTH_SHORT).show(); 
          } else { 
            c++; 
            Toast.makeText(AndroidCamera2API.this, "Face Found " + "\n", Toast.LENGTH_SHORT).show(); 
            //Toast.makeText(AndroidCamera2API.this, "Saved:" + file, Toast.LENGTH_SHORT).show(); 
            setFileToUpload(); 
            //file.delete(); 
            // if(file.exists()){ 
            //file.getCanonicalFile().delete(); 
            // if(file.exists()){ 
            //  getApplicationContext().deleteFile(file.getName()); 
            // } 
           // } 
           a++; 

            Toast.makeText(AndroidCamera2API.this, "" +c, Toast.LENGTH_SHORT).show(); 


           // Toast.makeText(AndroidCamera2API.this, "completed" , Toast.LENGTH_SHORT).show(); 


          } 
         } 
         }catch(FileNotFoundException e){ 
          e.printStackTrace(); 
         } catch(IOException e){ 
          e.printStackTrace(); 
         } finally{ 
          if (image != null) { 
           image.close(); 
          } 
         } 
        } 

je prendrai une photo dans OnResume:

protected void onResume() { 
     final Intent intent = new Intent(AndroidCamera2API.this, Completed.class); 
     super.onResume(); 
     Log.e(TAG, "onResume"); 

      startBackgroundThread(); 
      if (textureView.isAvailable()) { 
       openCamera(); 
      } else { 
       textureView.setSurfaceTextureListener(textureListener); 
      } 

      final int PICTURES_LIMIT = 15; 

      final Timer timer = new Timer(); 

       timer.schedule(new TimerTask() { 
        int pictureNo=0; 
        public void run() { 
         if (pictureNo>PICTURES_LIMIT) { 
          timer.cancel(); 
          startActivity(intent); 


         }else { 
          pictureNo++; 
          takePicture(); 


         } 
        } 


       }, 10, 7500); 


      } 

Répondre

0

Vous ne savez pas si cela est lié, mais vous ne réutilisez pas vos bitmaps après utilisation. Le Garbage Collector (GC) devrait prendre cela pour vous à partir de la version Android 3 et plus, mais vous en faites de nouveaux si rapidement peut-être qu'il n'a pas le temps. Je ne remarque aucune exception de MOO dans votre trace de pile, ce qui n'est probablement pas le cas, mais vous devriez quand même recycler afin que le GC n'ait pas à faire autant de travail.