@Override
  public void onSequenceChanged(final int[] idx) {
    sequenceView.setEnabled(false);

    Size input = new Size(MainScreen.getImageWidth(), MainScreen.getImageHeight());
    // int imagesAmount =
    // Integer.parseInt(PluginManager.getInstance().getFromSharedMem("amountofcapturedframes"+Long.toString(sessionID)));
    // ArrayList<byte []> compressed_frame = new ArrayList<byte []>();
    int minSize = 1000;
    if (mMinSize == 0) {
      minSize = 0;
    } else {
      minSize = input.getWidth() * input.getHeight() / mMinSize;
    }

    Size preview = new Size(mDisplayWidth, mDisplayHeight);
    try {
      mAlmaCLRShot.initialize(
          preview,
          mAngle,
          /*
           * sensitivity for objection detection
           *
           */
          mSensitivity - 15,
          /*
           *  Minimum size of object to be able to detect
           *  -15 ~ 15
           *  max -> easy detection dull detection
           *  min ->
           */
          minSize,
          /*
           * ghosting parameter
           * 0 : normal operation
           * 1 : detect ghosted objects but not remove them
           * 2 : detect and remove all object
           */
          Integer.parseInt(mGhosting),
          idx);
    } catch (NumberFormatException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    mHandler.sendEmptyMessage(MSG_REDRAW);
  }
  public void onSequenceChanged(final int[] idx) {
    sequenceView.setEnabled(false);

    CameraController.Size imageSize = CameraController.getCameraImageSize();
    Size input = new Size(imageSize.getWidth(), imageSize.getHeight());
    int minSize = 1000;
    if (mMinSize == 0) {
      minSize = 0;
    } else {
      minSize = input.getWidth() * input.getHeight() / mMinSize;
    }

    Size preview = new Size(mDisplayWidth, mDisplayHeight);
    try {
      mAlmaCLRShot.initialize(
          preview,
          mAngle,
          /*
           * sensitivity for objection detection
           */
          mSensitivity - 15,
          /*
           * Minimum size of object to be able to detect -15 ~ 15 max -> easy
           * detection dull detection min ->
           */
          minSize,
          /*
           * ghosting parameter 0 : normal operation 1 : detect ghosted
           * objects but not remove them 2 : detect and remove all object
           */
          Integer.parseInt(mGhosting),
          idx);
    } catch (NumberFormatException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }

    mHandler.sendEmptyMessage(MSG_REDRAW);
  }
  public void onStartProcessing(long SessionID) {
    finishing = false;
    Message msg = new Message();
    msg.what = ApplicationInterface.MSG_PROCESSING_BLOCK_UI;
    ApplicationScreen.getMessageHandler().sendMessage(msg);

    PluginManager.getInstance()
        .sendMessage(ApplicationInterface.MSG_BROADCAST, ApplicationInterface.MSG_CONTROL_LOCKED);

    ApplicationScreen.getGUIManager().lockControls = true;

    sessionID = SessionID;

    PluginManager.getInstance()
        .addToSharedMem(
            "modeSaveName" + sessionID, PluginManager.getInstance().getActiveMode().modeSaveName);

    mDisplayOrientation =
        Integer.valueOf(
            PluginManager.getInstance().getFromSharedMem("frameorientation1" + sessionID));
    int orientation = ApplicationScreen.getGUIManager().getLayoutOrientation();
    mLayoutOrientationCurrent =
        (orientation == 0 || orientation == 180) ? orientation : (orientation + 180) % 360;

    mCameraMirrored =
        Boolean.valueOf(PluginManager.getInstance().getFromSharedMem("framemirrored1" + sessionID));

    CameraController.Size imageSize = CameraController.getCameraImageSize();
    if (mDisplayOrientation == 0 || mDisplayOrientation == 180) {
      imgWidthOR = imageSize.getHeight();
      imgHeightOR = imageSize.getWidth();
    } else {
      imgWidthOR = imageSize.getWidth();
      imgHeightOR = imageSize.getHeight();
    }

    mAlmaCLRShot = AlmaCLRShot.getInstance();

    getPrefs();

    try {
      Size input = new Size(imageSize.getWidth(), imageSize.getHeight());
      int imagesAmount =
          Integer.parseInt(
              PluginManager.getInstance().getFromSharedMem("amountofcapturedframes" + sessionID));
      int minSize = 1000;
      if (mMinSize == 0) {
        minSize = 0;
      } else {
        minSize = input.getWidth() * input.getHeight() / mMinSize;
      }

      if (imagesAmount == 0) imagesAmount = 1;

      int iImageWidth = imageSize.getWidth();
      int iImageHeight = imageSize.getHeight();

      thumbnails.clear();
      int heightPixels = ApplicationScreen.getAppResources().getDisplayMetrics().heightPixels;
      for (int i = 1; i <= imagesAmount; i++) {
        thumbnails.add(
            Bitmap.createScaledBitmap(
                ImageConversion.decodeYUVfromBuffer(
                    mYUVBufferList.get(i - 1), iImageWidth, iImageHeight),
                heightPixels / imagesAmount,
                (int) (iImageHeight * (((float) heightPixels / imagesAmount) / iImageWidth)),
                false));
      }

      Display display =
          ((WindowManager) ApplicationScreen.instance.getSystemService(Context.WINDOW_SERVICE))
              .getDefaultDisplay();
      Point dis = new Point();
      display.getSize(dis);

      float imageRatio = (float) iImageWidth / (float) iImageHeight;
      float displayRatio = (float) dis.y / (float) dis.x;

      if (imageRatio > displayRatio) {
        mDisplayWidth = dis.y;
        mDisplayHeight = (int) ((float) dis.y / (float) imageRatio);
      } else {
        mDisplayWidth = (int) ((float) dis.x * (float) imageRatio);
        mDisplayHeight = dis.x;
      }

      Size preview = new Size(mDisplayWidth, mDisplayHeight);

      PluginManager.getInstance()
          .addToSharedMem("amountofresultframes" + sessionID, String.valueOf(imagesAmount));

      PluginManager.getInstance()
          .addToSharedMem("saveImageWidth" + sessionID, String.valueOf(imgWidthOR));
      PluginManager.getInstance()
          .addToSharedMem("saveImageHeight" + sessionID, String.valueOf(imgHeightOR));

      this.indexes = new int[imagesAmount];
      for (int i = 0; i < imagesAmount; i++) {
        this.indexes[i] = i;
      }

      // frames!!! should be taken from heap
      mAlmaCLRShot.addYUVInputFrame(mYUVBufferList, input);

      mAlmaCLRShot.initialize(
          preview,
          mAngle,
          /*
           * sensitivity for objection detection
           */
          mSensitivity - 15,
          /*
           * Minimum size of object to be able to detect -15 ~ 15 max -> easy
           * detection dull detection min ->
           */
          minSize,
          /*
           * ghosting parameter 0 : normal operation 1 : detect ghosted
           * objects but not remove them 2 : detect and remove all object
           */
          Integer.parseInt(mGhosting),
          indexes);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @Override
  public void onStartProcessing(long SessionID) {
    finishing = false;
    Message msg = new Message();
    msg.what = PluginManager.MSG_PROCESSING_BLOCK_UI;
    MainScreen.H.sendMessage(msg);

    Message msg2 = new Message();
    msg2.arg1 = PluginManager.MSG_CONTROL_LOCKED;
    msg2.what = PluginManager.MSG_BROADCAST;
    MainScreen.H.sendMessage(msg2);

    MainScreen.guiManager.lockControls = true;

    sessionID = SessionID;

    PluginManager.getInstance()
        .addToSharedMem(
            "modeSaveName" + Long.toString(sessionID),
            PluginManager.getInstance().getActiveMode().modeSaveName);

    mDisplayOrientation = MainScreen.guiManager.getDisplayOrientation();
    int orientation = MainScreen.guiManager.getLayoutOrientation();
    mLayoutOrientationCurrent =
        (orientation == 0 || orientation == 180) ? orientation : (orientation + 180) % 360;
    mCameraMirrored = MainScreen.getCameraMirrored();

    if (mDisplayOrientation == 0 || mDisplayOrientation == 180) {
      imgWidthOR = MainScreen.getImageHeight();
      imgHeightOR = MainScreen.getImageWidth();
    } else {
      imgWidthOR = MainScreen.getImageWidth();
      imgHeightOR = MainScreen.getImageHeight();
    }

    int iSaveImageWidth = MainScreen.getSaveImageWidth();
    int iSaveImageHeight = MainScreen.getSaveImageHeight();

    mAlmaCLRShot = AlmaCLRShot.getInstance();

    getPrefs();

    try {
      Size input = new Size(MainScreen.getImageWidth(), MainScreen.getImageHeight());
      int imagesAmount =
          Integer.parseInt(
              PluginManager.getInstance()
                  .getFromSharedMem("amountofcapturedframes" + Long.toString(sessionID)));
      ArrayList<byte[]> compressed_frame = new ArrayList<byte[]>();
      int minSize = 1000;
      if (mMinSize == 0) {
        minSize = 0;
      } else {
        minSize = input.getWidth() * input.getHeight() / mMinSize;
      }

      if (imagesAmount == 0) imagesAmount = 1;

      thumbnails.clear();
      for (int i = 1; i <= imagesAmount; i++) {
        byte[] in =
            SwapHeap.CopyFromHeap(
                Integer.parseInt(
                    PluginManager.getInstance()
                        .getFromSharedMem("frame" + i + Long.toString(sessionID))),
                Integer.parseInt(
                    PluginManager.getInstance()
                        .getFromSharedMem("framelen" + i + Long.toString(sessionID))));

        compressed_frame.add(i - 1, in);

        BitmapFactory.Options opts = new BitmapFactory.Options();
        thumbnails.add(
            Bitmap.createScaledBitmap(
                BitmapFactory.decodeByteArray(in, 0, in.length, opts),
                MainScreen.thiz.getResources().getDisplayMetrics().heightPixels / imagesAmount,
                (int)
                    (opts.outHeight
                        * (((float) MainScreen.thiz.getResources().getDisplayMetrics().heightPixels
                                / imagesAmount)
                            / opts.outWidth)),
                false));
      }

      mJpegBufferList = compressed_frame;
      getDisplaySize(mJpegBufferList.get(0));
      Size preview = new Size(mDisplayWidth, mDisplayHeight);

      if (SaveInputPreference) {
        try {
          File saveDir = PluginManager.getInstance().GetSaveDir(false);

          SharedPreferences prefs =
              PreferenceManager.getDefaultSharedPreferences(MainScreen.mainContext);
          int saveOption = Integer.parseInt(prefs.getString("exportName", "3"));
          Calendar d = Calendar.getInstance();
          String fileFormat =
              String.format(
                  "%04d%02d%02d_%02d%02d%02d",
                  d.get(Calendar.YEAR),
                  d.get(Calendar.MONTH) + 1,
                  d.get(Calendar.DAY_OF_MONTH),
                  d.get(Calendar.HOUR_OF_DAY),
                  d.get(Calendar.MINUTE),
                  d.get(Calendar.SECOND));
          switch (saveOption) {
            case 1: // YEARMMDD_HHMMSS
              break;

            case 2: // YEARMMDD_HHMMSS_MODE
              fileFormat += "_" + PluginManager.getInstance().getActiveMode().modeSaveName;
              break;

            case 3: // IMG_YEARMMDD_HHMMSS
              fileFormat = "IMG_" + fileFormat;
              break;

            case 4: // IMG_YEARMMDD_HHMMSS_MODE
              fileFormat =
                  "IMG_"
                      + fileFormat
                      + "_"
                      + PluginManager.getInstance().getActiveMode().modeSaveName;
              break;
          }

          ContentValues values = null;

          for (int i = 0; i < imagesAmount; ++i) {

            String index = String.format("_%02d", i);
            File file = new File(saveDir, fileFormat + index + ".jpg");

            FileOutputStream os = null;
            try {
              os = new FileOutputStream(file);
            } catch (Exception e) {
              // save always if not working saving to sdcard
              e.printStackTrace();
              saveDir = PluginManager.getInstance().GetSaveDir(true);
              file = new File(saveDir, fileFormat + index + ".jpg");
              os = new FileOutputStream(file);
            }

            String resultOrientation =
                PluginManager.getInstance()
                    .getFromSharedMem("frameorientation" + (i + 1) + Long.toString(sessionID));
            Boolean orientationLandscape = false;
            if (resultOrientation == null) orientationLandscape = true;
            else orientationLandscape = Boolean.parseBoolean(resultOrientation);

            String resultMirrored =
                PluginManager.getInstance()
                    .getFromSharedMem("framemirrored" + (i + 1) + Long.toString(sessionID));
            Boolean cameraMirrored = false;
            if (resultMirrored != null) cameraMirrored = Boolean.parseBoolean(resultMirrored);

            if (os != null) {
              // ToDo: not enough memory error reporting
              os.write(compressed_frame.get(i));
              os.close();

              ExifInterface ei = new ExifInterface(file.getAbsolutePath());
              int exif_orientation = ExifInterface.ORIENTATION_NORMAL;
              switch (mDisplayOrientation) {
                default:
                case 0:
                  exif_orientation =
                      ExifInterface
                          .ORIENTATION_NORMAL; // cameraMirrored ?
                                               // ExifInterface.ORIENTATION_ROTATE_180 :
                                               // ExifInterface.ORIENTATION_NORMAL;
                  break;
                case 90:
                  exif_orientation =
                      cameraMirrored
                          ? ExifInterface.ORIENTATION_ROTATE_270
                          : ExifInterface.ORIENTATION_ROTATE_90;
                  break;
                case 180:
                  exif_orientation =
                      ExifInterface
                          .ORIENTATION_ROTATE_180; // cameraMirrored ?
                                                   // ExifInterface.ORIENTATION_NORMAL :
                                                   // ExifInterface.ORIENTATION_ROTATE_180;
                  break;
                case 270:
                  exif_orientation =
                      cameraMirrored
                          ? ExifInterface.ORIENTATION_ROTATE_90
                          : ExifInterface.ORIENTATION_ROTATE_270;
                  break;
              }
              ei.setAttribute(ExifInterface.TAG_ORIENTATION, "" + exif_orientation);
              ei.saveAttributes();
            }

            String dateString = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").format(new Date());
            values = new ContentValues(9);
            values.put(
                ImageColumns.TITLE, file.getName().substring(0, file.getName().lastIndexOf(".")));
            values.put(ImageColumns.DISPLAY_NAME, file.getName());
            values.put(ImageColumns.DATE_TAKEN, System.currentTimeMillis());
            values.put(ImageColumns.MIME_TYPE, "image/jpeg");
            values.put(
                ImageColumns.ORIENTATION,
                (!orientationLandscape && !cameraMirrored)
                    ? 90
                    : (!orientationLandscape && cameraMirrored) ? -90 : 0);
            values.put(ImageColumns.DATA, file.getAbsolutePath());

            if (prefs.getBoolean("useGeoTaggingPrefExport", false)) {
              Location l = MLocation.getLocation(MainScreen.mainContext);

              if (l != null) {
                //    			            	Exiv2.writeGeoDataIntoImage(
                //    			            		file.getAbsolutePath(),
                //    			            		true,
                //    			            		l.getLatitude(),
                //    			            		l.getLongitude(),
                //    			            		dateString,
                //    			            		android.os.Build.MANUFACTURER != null ?
                // android.os.Build.MANUFACTURER : "Google",
                //    			            		android.os.Build.MODEL != null ? android.os.Build.MODEL :
                // "Android device");

                ExifInterface ei = new ExifInterface(file.getAbsolutePath());
                ei.setAttribute(
                    ExifInterface.TAG_GPS_LATITUDE, GPSTagsConverter.convert(l.getLatitude()));
                ei.setAttribute(
                    ExifInterface.TAG_GPS_LATITUDE_REF,
                    GPSTagsConverter.latitudeRef(l.getLatitude()));
                ei.setAttribute(
                    ExifInterface.TAG_GPS_LONGITUDE, GPSTagsConverter.convert(l.getLongitude()));
                ei.setAttribute(
                    ExifInterface.TAG_GPS_LONGITUDE_REF,
                    GPSTagsConverter.longitudeRef(l.getLongitude()));

                ei.saveAttributes();

                values.put(ImageColumns.LATITUDE, l.getLatitude());
                values.put(ImageColumns.LONGITUDE, l.getLongitude());
              }
            }

            MainScreen.thiz.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
          }
        } catch (IOException e) {
          e.printStackTrace();
          MainScreen.H.sendEmptyMessage(PluginManager.MSG_EXPORT_FINISHED_IOEXCEPTION);
          return;
        } catch (Exception e) {
          // Toast.makeText(MainScreen.mainContext, "Low memory. Can't finish processing.",
          // Toast.LENGTH_LONG).show();
          e.printStackTrace();
        }
      }

      PluginManager.getInstance()
          .addToSharedMem(
              "amountofresultframes" + Long.toString(sessionID), String.valueOf(imagesAmount));

      PluginManager.getInstance()
          .addToSharedMem(
              "saveImageWidth" + String.valueOf(sessionID), String.valueOf(iSaveImageWidth));
      PluginManager.getInstance()
          .addToSharedMem(
              "saveImageHeight" + String.valueOf(sessionID), String.valueOf(iSaveImageHeight));

      this.indexes = new int[imagesAmount];
      for (int i = 0; i < imagesAmount; i++) {
        this.indexes[i] = i;
      }

      // frames!!! should be taken from heap
      mAlmaCLRShot.addInputFrame(compressed_frame, input);

      mAlmaCLRShot.initialize(
          preview,
          mAngle,
          /*
           * sensitivity for objection detection
           *
           */
          mSensitivity - 15,
          /*
           *  Minimum size of object to be able to detect
           *  -15 ~ 15
           *  max -> easy detection dull detection
           *  min ->
           */
          minSize,
          /*
           * ghosting parameter
           * 0 : normal operation
           * 1 : detect ghosted objects but not remove them
           * 2 : detect and remove all object
           */
          Integer.parseInt(mGhosting),
          indexes);
      compressed_frame.clear();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }