Example usage for android.media ExifInterface ORIENTATION_NORMAL

List of usage examples for android.media ExifInterface ORIENTATION_NORMAL

Introduction

In this page you can find the example usage for android.media ExifInterface ORIENTATION_NORMAL.

Prototype

int ORIENTATION_NORMAL

To view the source code for android.media ExifInterface ORIENTATION_NORMAL.

Click Source Link

Usage

From source file:com.m2dl.mini_projet.mini_projet_android.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);

            ContentResolver cr = getContentResolver();
            try {
                myBitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
                myBitmap = BitmapUtil.resize(myBitmap);
                imageFilePath = imageUri.getPath();
                ExifInterface exif = new ExifInterface(imageFilePath);
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);
                switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    myBitmap = BitmapUtil.rotateImage(myBitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    myBitmap = BitmapUtil.rotateImage(myBitmap, 180);
                    break;
                }// w ww.j av a2  s  .  c  o  m
                String dialogTitle;
                String dialogMessage;
                if (!isGPSOn) {
                    dialogTitle = "Golocalisation GPS...";
                    dialogMessage = "Veuillez activer votre GPS puis patienter pendant la golocalisation";
                } else {
                    dialogTitle = "Golocalisation GPS...";
                    dialogMessage = "Veuillez patienter pendant la golocalisation";
                }
                final ProgressDialog progDialog = ProgressDialog.show(MainActivity.this, dialogTitle,
                        dialogMessage, true);
                new Thread() {
                    public void run() {
                        try {
                            while (coordLat.equals(0.0d) && coordLong.equals(0.0d))
                                ;
                            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                            Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
                            if (prev != null) {
                                ft.remove(prev);
                            }
                            ft.addToBackStack(null);
                            DialogFragment newFragment = PhotoDialogFragment.newInstance(myBitmap, coordLat,
                                    coordLong, imageFilePath);
                            newFragment.show(ft, "dialog");
                        } catch (Exception e) {
                            Log.e(TAG, e.getMessage());
                        }
                        progDialog.dismiss();
                    }
                }.start();
            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                Log.e("Camera", e.toString());
            }
        }
    }
}

From source file:com.liwn.zzl.markbit.DrawOptionsMenuActivity.java

private Uri rotateNormal(Uri uri) throws IOException {
    ExifInterface exif = new ExifInterface(uri.getPath());
    int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    int rotationInDegrees = exifToDegrees(rotation);
    Matrix matrix = new Matrix();
    if (rotation != 0) {
        matrix.preRotate(rotationInDegrees);
    }//from w ww  .  ja  va2s.  c om
    Bitmap srcBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
    Bitmap adjustedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, MarkBitApplication.BIT_LCD_WIDTH,
            MarkBitApplication.BIT_LCD_HEIGHT, matrix, true);
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    adjustedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), adjustedBitmap, "Title", null);
    return Uri.parse(path);
}

From source file:com.google.android.gms.samples.vision.face.photo.PhotoViewerActivity.java

public Bitmap grabImage() {
    this.getContentResolver().notifyChange(photoURI, null);
    ContentResolver cr = this.getContentResolver();
    Bitmap bitmap;/*from w  w  w .jav a2  s  .  c  om*/
    try {
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, photoURI);

        ExifInterface ei = new ExifInterface(mCurrentPhotoPath);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        switch (orientation) {

        case ExifInterface.ORIENTATION_ROTATE_90:
            bitmap = rotateImage(bitmap, 90);
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            bitmap = rotateImage(bitmap, 180);
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            bitmap = rotateImage(bitmap, 270);
            break;

        case ExifInterface.ORIENTATION_NORMAL:

        default:
            break;
        }
        return bitmap;
    } catch (Exception e) {
        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Failed to load", e);
        return null;
    }
}

From source file:com.spoiledmilk.ibikecph.util.Util.java

public static Bitmap bmpDecodeFile(File f, int width_limit, int height_limit, long max_size,
        boolean max_dimensions) {
    if (f == null) {
        return null;
    }//from  ww w  .j  a  v  a2 s  . c  om

    LOG.d("bmpDecodeFile(" + f.getAbsolutePath() + "," + width_limit + "," + height_limit + "," + max_size + ","
            + max_dimensions + ")");

    Bitmap bmp = null;
    boolean shouldReturn = false;

    FileInputStream fin = null;
    int orientation = ExifInterface.ORIENTATION_NORMAL;
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        fin = new FileInputStream(f);
        BitmapFactory.decodeStream(fin, null, o);
        try {
            fin.close();
            fin = null;
        } catch (IOException e) {
        }

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        if (width_limit != -1 && height_limit != -1) {
            if (max_dimensions) {
                while (o.outWidth / scale > width_limit || o.outHeight / scale > height_limit)
                    scale *= 2;
            } else {
                while (o.outWidth / scale / 2 >= width_limit && o.outHeight / scale / 2 >= height_limit)
                    scale *= 2;
            }
        } else if (max_size != -1)
            while ((o.outWidth * o.outHeight) / (scale * scale) > max_size)
                scale *= 2;

        // Decode with inSampleSize
        o = null;
        if (scale > 1) {
            o = new BitmapFactory.Options();
            o.inSampleSize = scale;
        }
        fin = new FileInputStream(f);
        try {
            bmp = BitmapFactory.decodeStream(fin, null, o);
        } catch (OutOfMemoryError e) {
            // Try to recover from out of memory error - but keep in mind
            // that behavior after this error is
            // undefined,
            // for example more out of memory errors might appear in catch
            // block.
            if (bmp != null)
                bmp.recycle();
            bmp = null;
            System.gc();
            LOG.e("Util.bmpDecodeFile() OutOfMemoryError in decodeStream()! Trying to recover...");
        }

        if (bmp != null) {
            LOG.d("resulting bitmap width : " + bmp.getWidth() + " height : " + bmp.getHeight() + " size : "
                    + (bmp.getRowBytes() * bmp.getHeight()));

            ExifInterface exif = new ExifInterface(f.getPath());
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        }
    } catch (FileNotFoundException e) {
        shouldReturn = true;
    } catch (IOException e) {
        shouldReturn = true; // bitmap is still valid here, just can't be
        // rotated
    } finally {
        if (fin != null)
            try {
                fin.close();
            } catch (IOException e) {
            }
    }

    if (shouldReturn || bmp == null)
        return bmp;

    float rotate = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotate = 90;
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotate = 180;
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotate = 270;
        break;
    }
    if (rotate > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);
        Bitmap bmpRot = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
        matrix = null;
        bmp.recycle();
        bmp = null;
        // System.gc();
        return bmpRot;
    }

    return bmp;
}

From source file:com.allen.mediautil.ImageTakerHelper.java

/**
 * ?/*from   ww  w  . j  a  v  a  2s .c o  m*/
 *
 * @param path ?
 * @return degree
 */
private static int readPictureDegree(String path) {
    int degree = 0;
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degree = 90;// SUPPRESS CHECKSTYLE
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degree = 180;// SUPPRESS CHECKSTYLE
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degree = 270;// SUPPRESS CHECKSTYLE
            break;
        default:
            break;
        }
    } catch (IOException e) {
        Log.e("readPictureDegree", "readPictureDegree failed", e);
    }
    return degree;
}

From source file:com.yanzhenjie.album.task.LocalImageLoader.java

/**
 * Read the rotation angle of the picture file.
 *
 * @param path image path.// ww  w .j  a  v  a  2 s  . co m
 * @return one of 0, 90, 180, 270.
 */
public static int readDegree(String path) {
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            return 90;
        case ExifInterface.ORIENTATION_ROTATE_180:
            return 180;
        case ExifInterface.ORIENTATION_ROTATE_270:
            return 270;
        default:
            return 0;
        }
    } catch (Exception e) {
        return 0;
    }
}

From source file:com.silentcircle.common.util.ViewUtil.java

/**
 * Finds JPEG image rotation flags from exif and returns matrix to be used to rotate image.
 *
 * @param fileName JPEG image file name.
 *
 * @return Matrix to use in image rotate transformation or null if parsing failed.
 *///  w  ww . j ava2 s  .com
public static Matrix getRotationMatrixFromExif(final String fileName) {
    Matrix matrix = null;
    try {
        ExifInterface exif = new ExifInterface(fileName);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = ROTATE_90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = ROTATE_180;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = ROTATE_270;
            break;
        }

        if (rotate != 0) {
            matrix = new Matrix();
            matrix.preRotate(rotate);
        }
    } catch (IOException e) {
        Log.i(TAG, "Failed to determine image flags from file " + fileName);
    }
    return matrix;
}

From source file:com.almalence.opencam.PluginManager.java

@Override
protected void createPlugins() {
    pluginList = new Hashtable<String, Plugin>();
    processingPluginList = new HashMap<Long, Plugin>();

    sharedMemory = new Hashtable<String, String>();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        createRAWCaptureResultHashtable();

    activeVF = new ArrayList<String>();
    // activeFilter = new ArrayList<String>();

    listVF = new ArrayList<Plugin>();
    listCapture = new ArrayList<Plugin>();
    listProcessing = new ArrayList<Plugin>();
    // listFilter = new ArrayList<Plugin>();
    listExport = new ArrayList<Plugin>();

    // init plugins and add to pluginList
    /*//w w  w.  j av a2 s.c om
     * Insert any new plugin below (create and add to list of concrete type)
     */

    // VF
    AeAwLockVFPlugin aeawlockVFPlugin = new AeAwLockVFPlugin();
    pluginList.put(aeawlockVFPlugin.getID(), aeawlockVFPlugin);
    listVF.add(aeawlockVFPlugin);

    HistogramVFPlugin histgramVFPlugin = new HistogramVFPlugin();
    pluginList.put(histgramVFPlugin.getID(), histgramVFPlugin);
    listVF.add(histgramVFPlugin);

    BarcodeScannerVFPlugin barcodeScannerVFPlugin = new BarcodeScannerVFPlugin();
    pluginList.put(barcodeScannerVFPlugin.getID(), barcodeScannerVFPlugin);
    listVF.add(barcodeScannerVFPlugin);

    GyroVFPlugin gyroVFPlugin = new GyroVFPlugin();
    pluginList.put(gyroVFPlugin.getID(), gyroVFPlugin);
    listVF.add(gyroVFPlugin);

    ZoomVFPlugin zoomVFPlugin = new ZoomVFPlugin();
    pluginList.put(zoomVFPlugin.getID(), zoomVFPlugin);
    listVF.add(zoomVFPlugin);

    GridVFPlugin gridVFPlugin = new GridVFPlugin();
    pluginList.put(gridVFPlugin.getID(), gridVFPlugin);
    listVF.add(gridVFPlugin);

    FocusVFPlugin focusVFPlugin = new FocusVFPlugin();
    pluginList.put(focusVFPlugin.getID(), focusVFPlugin);
    listVF.add(focusVFPlugin);

    InfosetVFPlugin infosetVFPlugin = new InfosetVFPlugin();
    pluginList.put(infosetVFPlugin.getID(), infosetVFPlugin);
    listVF.add(infosetVFPlugin);

    // Capture
    CapturePlugin testCapturePlugin = new CapturePlugin();
    pluginList.put(testCapturePlugin.getID(), testCapturePlugin);
    listCapture.add(testCapturePlugin);

    ExpoBracketingCapturePlugin expoBracketingCapturePlugin = new ExpoBracketingCapturePlugin();
    pluginList.put(expoBracketingCapturePlugin.getID(), expoBracketingCapturePlugin);
    listCapture.add(expoBracketingCapturePlugin);

    NightCapturePlugin nightCapturePlugin = new NightCapturePlugin();
    pluginList.put(nightCapturePlugin.getID(), nightCapturePlugin);
    listCapture.add(nightCapturePlugin);

    BurstCapturePlugin burstCapturePlugin = new BurstCapturePlugin();
    pluginList.put(burstCapturePlugin.getID(), burstCapturePlugin);
    listCapture.add(burstCapturePlugin);

    BestShotCapturePlugin bestShotCapturePlugin = new BestShotCapturePlugin();
    pluginList.put(bestShotCapturePlugin.getID(), bestShotCapturePlugin);
    listCapture.add(bestShotCapturePlugin);

    MultiShotCapturePlugin multiShotCapturePlugin = new MultiShotCapturePlugin();
    pluginList.put(multiShotCapturePlugin.getID(), multiShotCapturePlugin);
    listCapture.add(multiShotCapturePlugin);

    VideoCapturePlugin videoCapturePlugin = new VideoCapturePlugin();
    pluginList.put(videoCapturePlugin.getID(), videoCapturePlugin);
    listCapture.add(videoCapturePlugin);

    PreshotCapturePlugin backInTimeCapturePlugin = new PreshotCapturePlugin();
    pluginList.put(backInTimeCapturePlugin.getID(), backInTimeCapturePlugin);
    listCapture.add(backInTimeCapturePlugin);

    PanoramaAugmentedCapturePlugin panoramaAugmentedCapturePlugin = new PanoramaAugmentedCapturePlugin();
    pluginList.put(panoramaAugmentedCapturePlugin.getID(), panoramaAugmentedCapturePlugin);
    listCapture.add(panoramaAugmentedCapturePlugin);

    PreshotProcessingPlugin backInTimeProcessingPlugin = new PreshotProcessingPlugin();
    pluginList.put(backInTimeProcessingPlugin.getID(), backInTimeProcessingPlugin);
    listCapture.add(backInTimeProcessingPlugin);

    // Processing
    SimpleProcessingPlugin simpleProcessingPlugin = new SimpleProcessingPlugin();
    pluginList.put(simpleProcessingPlugin.getID(), simpleProcessingPlugin);
    listProcessing.add(simpleProcessingPlugin);

    NightProcessingPlugin nightProcessingPlugin = new NightProcessingPlugin();
    pluginList.put(nightProcessingPlugin.getID(), nightProcessingPlugin);
    listProcessing.add(nightProcessingPlugin);

    HDRProcessingPlugin hdrProcessingPlugin = new HDRProcessingPlugin();
    pluginList.put(hdrProcessingPlugin.getID(), hdrProcessingPlugin);
    listProcessing.add(hdrProcessingPlugin);

    MultiShotProcessingPlugin multiShotProcessingPlugin = new MultiShotProcessingPlugin();
    pluginList.put(multiShotProcessingPlugin.getID(), multiShotProcessingPlugin);
    listProcessing.add(multiShotProcessingPlugin);

    PanoramaProcessingPlugin panoramaProcessingPlugin = new PanoramaProcessingPlugin();
    pluginList.put(panoramaProcessingPlugin.getID(), panoramaProcessingPlugin);
    listProcessing.add(panoramaProcessingPlugin);

    BestshotProcessingPlugin bestshotProcessingPlugin = new BestshotProcessingPlugin();
    pluginList.put(bestshotProcessingPlugin.getID(), bestshotProcessingPlugin);
    listProcessing.add(bestshotProcessingPlugin);

    // Filter

    // Export
    ExportPlugin testExportPlugin = new ExportPlugin();
    pluginList.put(testExportPlugin.getID(), testExportPlugin);
    listExport.add(testExportPlugin);

    // parsing configuration file to setup modes
    parseConfig();

    exifOrientationMap = new HashMap<Integer, Integer>() {
        {
            put(0, ExifInterface.ORIENTATION_NORMAL);
            put(90, ExifInterface.ORIENTATION_ROTATE_90);
            put(180, ExifInterface.ORIENTATION_ROTATE_180);
            put(270, ExifInterface.ORIENTATION_ROTATE_270);
        }
    };
}

From source file:com.example.photoremark.MainActivity.java

/**
 * ?exif?//from   w ww .  ja  va 2 s .  co m
 *
 * @param path 
 * @return
 */
public int getPictureDegree(String path) {
    int degree = 0;
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degree = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degree = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degree = 270;
            break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return degree;
}

From source file:com.almalence.opencam.SavingService.java

public void saveResultPicture(long sessionID) {
    initSavingPrefs();//from   w w w. ja  v a2 s.  c o m
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    // save fused result
    try {
        File saveDir = getSaveDir(false);

        Calendar d = Calendar.getInstance();

        int imagesAmount = Integer
                .parseInt(getFromSharedMem("amountofresultframes" + Long.toString(sessionID)));

        if (imagesAmount == 0)
            imagesAmount = 1;

        int imageIndex = 0;
        String sImageIndex = getFromSharedMem("resultframeindex" + Long.toString(sessionID));
        if (sImageIndex != null)
            imageIndex = Integer.parseInt(getFromSharedMem("resultframeindex" + Long.toString(sessionID)));

        if (imageIndex != 0)
            imagesAmount = 1;

        ContentValues values = null;

        boolean hasDNGResult = false;
        for (int i = 1; i <= imagesAmount; i++) {
            hasDNGResult = false;
            String format = getFromSharedMem("resultframeformat" + i + Long.toString(sessionID));

            if (format != null && format.equalsIgnoreCase("dng"))
                hasDNGResult = true;

            String idx = "";

            if (imagesAmount != 1)
                idx += "_" + ((format != null && !format.equalsIgnoreCase("dng") && hasDNGResult)
                        ? i - imagesAmount / 2
                        : i);

            String modeName = getFromSharedMem("modeSaveName" + Long.toString(sessionID));
            // define file name format. from settings!
            String fileFormat = getExportFileName(modeName);
            fileFormat += idx + ((format != null && format.equalsIgnoreCase("dng")) ? ".dng" : ".jpg");

            File file;
            if (ApplicationScreen.getForceFilename() == null) {
                file = new File(saveDir, fileFormat);
            } else {
                file = ApplicationScreen.getForceFilename();
            }

            OutputStream os = null;
            if (ApplicationScreen.getForceFilename() != null) {
                os = getApplicationContext().getContentResolver()
                        .openOutputStream(ApplicationScreen.getForceFilenameURI());
            } else {
                try {
                    os = new FileOutputStream(file);
                } catch (Exception e) {
                    // save always if not working saving to sdcard
                    e.printStackTrace();
                    saveDir = getSaveDir(true);
                    if (ApplicationScreen.getForceFilename() == null) {
                        file = new File(saveDir, fileFormat);
                    } else {
                        file = ApplicationScreen.getForceFilename();
                    }
                    os = new FileOutputStream(file);
                }
            }

            // Take only one result frame from several results
            // Used for PreShot plugin that may decide which result to save
            if (imagesAmount == 1 && imageIndex != 0)
                i = imageIndex;

            String resultOrientation = getFromSharedMem(
                    "resultframeorientation" + i + Long.toString(sessionID));
            int orientation = 0;
            if (resultOrientation != null)
                orientation = Integer.parseInt(resultOrientation);

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

            int x = Integer.parseInt(getFromSharedMem("saveImageHeight" + Long.toString(sessionID)));
            int y = Integer.parseInt(getFromSharedMem("saveImageWidth" + Long.toString(sessionID)));
            if (orientation == 0 || orientation == 180 || (format != null && format.equalsIgnoreCase("dng"))) {
                x = Integer.valueOf(getFromSharedMem("saveImageWidth" + Long.toString(sessionID)));
                y = Integer.valueOf(getFromSharedMem("saveImageHeight" + Long.toString(sessionID)));
            }

            Boolean writeOrientationTag = true;
            String writeOrientTag = getFromSharedMem("writeorientationtag" + Long.toString(sessionID));
            if (writeOrientTag != null)
                writeOrientationTag = Boolean.parseBoolean(writeOrientTag);

            if (format != null && format.equalsIgnoreCase("jpeg")) {// if result in jpeg format

                if (os != null) {
                    byte[] frame = SwapHeap.SwapFromHeap(
                            Integer.parseInt(getFromSharedMem("resultframe" + i + Long.toString(sessionID))),
                            Integer.parseInt(
                                    getFromSharedMem("resultframelen" + i + Long.toString(sessionID))));
                    os.write(frame);
                    try {
                        os.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else if (format != null && format.equalsIgnoreCase("dng")) {
                saveDNGPicture(i, sessionID, os, x, y, orientation, cameraMirrored);
            } else {// if result in nv21 format
                int yuv = Integer.parseInt(getFromSharedMem("resultframe" + i + Long.toString(sessionID)));
                com.almalence.YuvImage out = new com.almalence.YuvImage(yuv, ImageFormat.NV21, x, y, null);
                Rect r;

                String res = getFromSharedMem("resultfromshared" + Long.toString(sessionID));
                if ((null == res) || "".equals(res) || "true".equals(res)) {
                    // to avoid problems with SKIA
                    int cropHeight = out.getHeight() - out.getHeight() % 16;
                    r = new Rect(0, 0, out.getWidth(), cropHeight);
                } else {
                    if (null == getFromSharedMem("resultcrop0" + Long.toString(sessionID))) {
                        // to avoid problems with SKIA
                        int cropHeight = out.getHeight() - out.getHeight() % 16;
                        r = new Rect(0, 0, out.getWidth(), cropHeight);
                    } else {
                        int crop0 = Integer
                                .parseInt(getFromSharedMem("resultcrop0" + Long.toString(sessionID)));
                        int crop1 = Integer
                                .parseInt(getFromSharedMem("resultcrop1" + Long.toString(sessionID)));
                        int crop2 = Integer
                                .parseInt(getFromSharedMem("resultcrop2" + Long.toString(sessionID)));
                        int crop3 = Integer
                                .parseInt(getFromSharedMem("resultcrop3" + Long.toString(sessionID)));

                        r = new Rect(crop0, crop1, crop0 + crop2, crop1 + crop3);

                    }
                }

                jpegQuality = Integer.parseInt(prefs.getString(ApplicationScreen.sJPEGQualityPref, "95"));
                if (!out.compressToJpeg(r, jpegQuality, os)) {
                    if (ApplicationScreen.instance != null && ApplicationScreen.getMessageHandler() != null) {
                        ApplicationScreen.getMessageHandler()
                                .sendEmptyMessage(ApplicationInterface.MSG_EXPORT_FINISHED_IOEXCEPTION);
                    }
                    return;
                }
                SwapHeap.FreeFromHeap(yuv);
            }

            String orientation_tag = String.valueOf(0);
            //            int sensorOrientation = CameraController.getSensorOrientation();
            //            int displayOrientation = CameraController.getDisplayOrientation();
            //            sensorOrientation = (360 + sensorOrientation + (cameraMirrored ? -displayOrientation
            //                  : displayOrientation)) % 360;

            //            if (CameraController.isFlippedSensorDevice() && cameraMirrored)
            //               orientation = (orientation + 180) % 360;

            switch (orientation) {
            default:
            case 0:
                orientation_tag = String.valueOf(0);
                break;
            case 90:
                orientation_tag = cameraMirrored ? String.valueOf(270) : String.valueOf(90);
                break;
            case 180:
                orientation_tag = String.valueOf(180);
                break;
            case 270:
                orientation_tag = cameraMirrored ? String.valueOf(90) : String.valueOf(270);
                break;
            }

            int exif_orientation = ExifInterface.ORIENTATION_NORMAL;
            if (writeOrientationTag) {
                switch ((orientation + 360) % 360) {
                default:
                case 0:
                    exif_orientation = 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;
                    break;
                case 270:
                    exif_orientation = cameraMirrored ? ExifInterface.ORIENTATION_ROTATE_90
                            : ExifInterface.ORIENTATION_ROTATE_270;
                    break;
                }
            } else {
                switch ((additionalRotationValue + 360) % 360) {
                default:
                case 0:
                    exif_orientation = 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;
                    break;
                case 270:
                    exif_orientation = cameraMirrored ? ExifInterface.ORIENTATION_ROTATE_90
                            : ExifInterface.ORIENTATION_ROTATE_270;
                    break;
                }
            }

            if (!enableExifTagOrientation)
                exif_orientation = ExifInterface.ORIENTATION_NORMAL;

            File parent = file.getParentFile();
            String path = parent.toString().toLowerCase();
            String name = parent.getName().toLowerCase();

            values = new ContentValues();
            values.put(ImageColumns.TITLE,
                    file.getName().substring(0,
                            file.getName().lastIndexOf(".") >= 0 ? file.getName().lastIndexOf(".")
                                    : file.getName().length()));
            values.put(ImageColumns.DISPLAY_NAME, file.getName());
            values.put(ImageColumns.DATE_TAKEN, System.currentTimeMillis());
            values.put(ImageColumns.MIME_TYPE, "image/jpeg");

            if (enableExifTagOrientation) {
                if (writeOrientationTag) {
                    values.put(ImageColumns.ORIENTATION, String.valueOf(
                            (Integer.parseInt(orientation_tag) + additionalRotationValue + 360) % 360));
                } else {
                    values.put(ImageColumns.ORIENTATION, String.valueOf((additionalRotationValue + 360) % 360));
                }
            } else {
                values.put(ImageColumns.ORIENTATION, String.valueOf(0));
            }

            values.put(ImageColumns.BUCKET_ID, path.hashCode());
            values.put(ImageColumns.BUCKET_DISPLAY_NAME, name);
            values.put(ImageColumns.DATA, file.getAbsolutePath());

            File tmpFile;
            if (ApplicationScreen.getForceFilename() == null) {
                tmpFile = file;
            } else {
                tmpFile = new File(getApplicationContext().getFilesDir(), "buffer.jpeg");
                tmpFile.createNewFile();
                copyFromForceFileName(tmpFile);
            }

            if (!enableExifTagOrientation) {
                Matrix matrix = new Matrix();
                if (writeOrientationTag && (orientation + additionalRotationValue) != 0) {
                    matrix.postRotate((orientation + additionalRotationValue + 360) % 360);
                    rotateImage(tmpFile, matrix);
                } else if (!writeOrientationTag && additionalRotationValue != 0) {
                    matrix.postRotate((additionalRotationValue + 360) % 360);
                    rotateImage(tmpFile, matrix);
                }
            }

            if (useGeoTaggingPrefExport) {
                Location l = MLocation.getLocation(getApplicationContext());
                if (l != null) {
                    double lat = l.getLatitude();
                    double lon = l.getLongitude();
                    boolean hasLatLon = (lat != 0.0d) || (lon != 0.0d);
                    if (hasLatLon) {
                        values.put(ImageColumns.LATITUDE, l.getLatitude());
                        values.put(ImageColumns.LONGITUDE, l.getLongitude());
                    }
                }
            }

            File modifiedFile = saveExifTags(tmpFile, sessionID, i, x, y, exif_orientation,
                    useGeoTaggingPrefExport, enableExifTagOrientation);
            if (ApplicationScreen.getForceFilename() == null) {
                file.delete();
                modifiedFile.renameTo(file);
            } else {
                copyToForceFileName(modifiedFile);
                tmpFile.delete();
                modifiedFile.delete();
            }

            Uri uri = getApplicationContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI,
                    values);
            broadcastNewPicture(uri);
        }

        ApplicationScreen.getMessageHandler().sendEmptyMessage(ApplicationInterface.MSG_EXPORT_FINISHED);
    } catch (IOException e) {
        e.printStackTrace();
        ApplicationScreen.getMessageHandler()
                .sendEmptyMessage(ApplicationInterface.MSG_EXPORT_FINISHED_IOEXCEPTION);
        return;
    } catch (Exception e) {
        e.printStackTrace();
        ApplicationScreen.getMessageHandler().sendEmptyMessage(ApplicationInterface.MSG_EXPORT_FINISHED);
    } finally {
        ApplicationScreen.setForceFilename(null);
    }
}