question

hosseintavakoli-7723 avatar image
0 Votes"
hosseintavakoli-7723 asked cheong00 commented

enable Auto rotation photo camera2basic

Hi,
I useing Camera2basic.
I take picture in two format. Orignal And Timestamp Watermark.
When I take a orignal photo auto-rotation work correctly, but when I write date and time on photo auto-rotation not working for me.

my code :

 public void Run()
             {
                 ByteBuffer buffer = mImage.GetPlanes()[0].Buffer;
    
    
                 byte[] bytes = new byte[buffer.Remaining()];
                 buffer.Get(bytes);
                 using (var output = new FileOutputStream(mFile))
                 {
                     try
                     {
                         #region WatermarkeOnPhoto
    
                         if (AppInfo.PrintTimeStamp)
                         {
                             Android.Graphics.Bitmap OrignalImage;
                             Android.Graphics.Bitmap WatermarkedImage;
                             OrignalImage = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length); // Load your bitmap here
    
                             bytes = null;
                             WatermarkedImage = OrignalImage.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);
                             Canvas canvas = new Canvas(WatermarkedImage);
    
                             Paint paint = new Paint();
                             paint.Color = Android.Graphics.Color.Orange;
                             paint.TextSize = 50;
                             canvas.DrawText("Some", 50, 50, paint);
    
                             using (var stream = new System.IO.MemoryStream())
                             {
                                 WatermarkedImage.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
                                 bytes = stream.ToArray();
                             }
                         }
                         #endregion WatermarkeOnPhoto
    
                         output.Write(bytes);
                     }
                     catch (IOException e)
                     {
                         e.PrintStackTrace();
                     }
                     finally
                     {
                         mImage.Close();
                     }
                 }
             }


[2]: /answers/storage/attachments/80264-home-staging-worth-it-1024x576.jpg

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @hossein, what's the GetPlanes in your code?Could you please post a basic demo so that we can test on our side?

0 Votes 0 ·

GetPlanes is part of camera2basic, I don't add it.
you can see camera2basic here.

I add code between #region WatermarkeOnPhoto and #endregion WatermarkeOnPhoto nothing else.




0 Votes 0 ·
hosseintavakoli-7723 avatar image
0 Votes"
hosseintavakoli-7723 answered cheong00 commented

I don't change EXIF metadata.

here is my edit code :

 public void Run()
             {
                 ByteBuffer buffer = mImage.GetPlanes()[0].Buffer;
    
    
                 byte[] bytes = new byte[buffer.Remaining()];
                 buffer.Get(bytes);
                 using (var output = new FileOutputStream(mFile))
                 {
                     try
                     {
                         #region WatermarkeOnPhoto
    
                         if (AppInfo.PrintTimeStamp)
                         {
                             Android.Graphics.Bitmap OrignalImage;
                             Android.Graphics.Bitmap WatermarkedImage;
                             OrignalImage = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length); // Load your bitmap here
    
                             bytes = null;
                             WatermarkedImage = OrignalImage.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);
                             Canvas canvas = new Canvas(WatermarkedImage);
    
                             Paint paint = new Paint();
                             paint.Color = Android.Graphics.Color.Orange;
                             paint.TextSize = 50;
                             canvas.DrawText("Some", 50, 50, paint);
    
                             using (var stream = new System.IO.MemoryStream())
                             {
                                 WatermarkedImage.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
                                 bytes = stream.ToArray();
                             }
                         }
                         #endregion WatermarkeOnPhoto
    
                         output.Write(bytes);
                     }
                     catch (IOException e)
                     {
                         e.PrintStackTrace();
                     }
                     finally
                     {
                         mImage.Close();
                     }
                 }
             }


when AppInfo.PrintTimeStamp = true picture rotation not working, but when AppInfo.PrintTimeStamp = false in working great.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The bytes you get comes from WatermarkedImage.Compress(), and I believe this function won't automatically help you add back any EXIF data your original image contains. That's why auto-rotation won't work on the compressed image, as the image viewer software now won't have any idea on what angle it should rotate.

0 Votes 0 ·
JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered

Hello,


Welcome to our Microsoft Q&A platform!

Yes, you can try to use ExifInterface to achieve this.

please refer to the following code, though it is java code, it is easy to convert to c#.

 @Override
 public void onPictureTaken(byte[] data, Camera camera) {
    
     String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss").format( new Date( ));
     output_file_name = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + timeStamp + ".jpeg";
    
     File pictureFile = new File(output_file_name);
     if (pictureFile.exists()) {
         pictureFile.delete();
     }
    
     try {
         FileOutputStream fos = new FileOutputStream(pictureFile);
    
         Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
    
         ExifInterface exif=new ExifInterface(pictureFile.toString());
    
         Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
         if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
             realImage= rotate(realImage, 90);
         } else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
             realImage= rotate(realImage, 270);
         } else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
             realImage= rotate(realImage, 180);
         } else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")){
             realImage= rotate(realImage, 90);
         }
    
         boolean bo = realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    
         fos.close();
    
         ((ImageView) findViewById(R.id.imageview)).setImageBitmap(realImage);
    
         Log.d("Info", bo + "");
    
     } catch (FileNotFoundException e) {
         Log.d("Info", "File not found: " + e.getMessage());
     } catch (IOException e) {
         Log.d("TAG", "Error accessing file: " + e.getMessage());
     }
 }
    
 public static Bitmap rotate(Bitmap bitmap, int degree) {
     int w = bitmap.getWidth();
     int h = bitmap.getHeight();
    
     Matrix mtx = new Matrix();
    //       mtx.postRotate(degree);
     mtx.setRotate(degree);
    
     return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
 }

Refer : https://stackoverflow.com/questions/15808719/controlling-the-camera-to-take-pictures-in-portrait-doesnt-rotate-the-final-ima



Best Regards,

Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.


Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cheong00 avatar image
0 Votes"
cheong00 answered

I believe the Exif metadata containing the angle of photo is lost during compression and you have to add it again by yourself.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.