2

I have a method to take a photo, take the sample of the vertical form just as I want, the horizontal lap and save the vertical photograph just as I wish for the exercise exercises, Activity called ViewerActivity and it is here Where the problem begins at the moment of displaying in an ImageView the photograph the horizontal sample: / he is reading and there is the class ExifInterface that allows to control the orientation of the image, but at the moment of displaying the Image is still horizontal

This class is in charge of sending the photo.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        try {
            ExifInterface exifInterface = new ExifInterface(photoFile.getAbsolutePath());
            int valor = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (valor) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    orientation = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    orientation = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    orientation = 270;
                    break;
            }
        } catch (Exception e) {
            Log.d("mensaje", e.toString());
        }
        Intent i = new Intent(this, ViewerActivity.class);
        i.putExtra(EXTRA_PHOTO_PATH, photoFile.getAbsolutePath());
        i.putExtra(EXTRA_PHOTO_ORIENTATION, orientation);
        i.putExtra(EXTRA_PHOTO_EDIT, false);
        startActivityForResult(i, 10);

This ViewerActivity class displays the photo and then sends it

private void sendPicture() {
    Intent intent = new Intent();
    intent.putExtra("path", localPath);
    intent.putExtra("orientation",orientation);
    setResult(Activity.RESULT_OK, intent);
    finish();
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Devix
  • 432
  • 5
  • 16

4 Answers4

1

Use RotateLayout ,complete class and sample is available in below link.

https://github.com/rongi/rotate-layout

use of RotateLayout is quite easy and less memory consumption compare to make new rotated Bitmap using Matrix.

place your ImageView inside RotateLayout like below Code

<com.github.rongi.rotate_layout.layout.RotateLayout
  android:id="@+id/form3_container"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:angle="180">

  <ImageView
   android:id="@+id/imageview"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />
</com.github.rongi.rotate_layout.layout.RotateLayout>

And Set orientation value that is get through ExifInterface into angle property in RotateLayout.And ImageView Rotated and you do not need to worry for bitmap or any other thing for ExifInterface orientation.

You can set Angle value dynamically from java code ,into RotateLayout Object like below

rotateLayout.setAngle(newAngle);
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • Hi friend can also add a targeting to the image not? And would it work the same? whit this method imageView.setRotation(90); – Devix Feb 15 '17 at 16:18
  • @JoseAnguiano yes imageView.setRotation(90) working but When ,Bitmap Exif orientation is Landscape and When we rotate to 90 then , Aspect Ratio of Bitmap within Image view not maintained – Chetan Joshi Feb 16 '17 at 05:34
0

Try this:

BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);

BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
ExifInterface exif = new ExifInterface(file);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;

int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;

Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);`

where file is the name of the image file.

  • Hello, but there arises my other doubt regarding this method, can be implemented in the part when I capture the photo, or when I already send it to the imageView? Thanks for your support – Devix Feb 15 '17 at 05:20
  • Convert the result bitmap to a file and do this. – Jasmine Thomas Feb 15 '17 at 05:34
0
try {
File f = new File(imagePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
        ExifInterface.TAG_ORIENTATION,
        ExifInterface.ORIENTATION_NORMAL);

int angle = 0;

if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
    angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
    angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
    angle = 270;
}

Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;

Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
        null, options);
bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
        bmp.getHeight(), mat, true);
ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100,
        outstudentstreamOutputStream);
imageView.setImageBitmap(bitmap);

} catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}

try this when you get the imagepath in onActivityResult

aj0822ArpitJoshi
  • 1,142
  • 1
  • 9
  • 25
  • Hello friend at the moment I'm implementing the method let me see how it comes out and I'll tell you thanks – Devix Feb 15 '17 at 06:03
0

Try it

    public Bitmap rotateImage() {
    Bitmap scaled = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        Bitmap bm = BitmapFactory.decodeStream(fis);

        ExifInterface exif = new ExifInterface(file.getAbsolutePath());

        String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
        int rotationAngle = 0;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
            rotationAngle = 90;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
            rotationAngle = 180;
        if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
            rotationAngle = 270;
        Matrix matrix = new Matrix();
        matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        int height = rotatedBitmap.getHeight(), scaledheight, scaledwidth;
        int width = rotatedBitmap.getWidth();
        float aspect;

        if (width < height) {// portrait
            aspect = ((float) height / (float) width);
            scaledwidth = 400;
            scaledheight = (int) (400 * aspect);
        } else {// landscape
            aspect = ((float) width / (float) height);
            scaledheight = 400;
            scaledwidth = (int) (400 * aspect);
        }
        scaled = Bitmap.createScaledBitmap(rotatedBitmap, scaledwidth, scaledheight, false);

        File f = new File(getCacheDir(), "scaledBitmap");
        f.createNewFile();

        Bitmap bitmap = scaled;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0 /* ignored for PNG */, bos);
        byte[] bitmapdata = bos.toByteArray();

        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();

        file = f;

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("photofile io error", "EXif not done");
    }
    return scaled;
}