Friday 15 March 2013

android - Xamarin.Forms Take photo with camera shows wrong orientation and crashes on back button -



android - Xamarin.Forms Take photo with camera shows wrong orientation and crashes on back button -

i using xamarin.forms photographic camera sample here - https://github.com/xforms/xamarin-forms-labs-samples/tree/master/xf.labs.camerasample able select or take photo.

private async task selectpicture() { mediapicker = dependencyservice.get<imediapicker>(); imagesource = null; var mediafile = await mediapicker.selectphotoasync(new cameramediastorageoptions { defaultcamera = cameradevice.front, maxpixeldimension = 400 }); imagesource = imagesource.fromstream(() => mediafile.source); img.source = imagesource; } private async task takepicture() { mediapicker = dependencyservice.get<imediapicker>(); imagesource = null; var mediafile = await mediapicker.takephotoasync(new cameramediastorageoptions { defaultcamera = cameradevice.front, maxpixeldimension = 400 }); imagesource = imagesource.fromstream(() => mediafile.source); img.source = imagesource; }

the code image simply

img = new image { backgroundcolor = color.white, aspect = aspect.aspectfit };

there couple of issues:

first one. can take photo or select stored 1 , show on page. if select photo displays correctly, either portrait or landscape. when take photo, displays in landscape mode, if image taken in portrait, image shows on side. isn't catastrophic, improve show image how taken.

the sec issue bit more drastic, if press device's button when either in photographic camera or image gallery screen goes blank , message stating app has stopped responding.

i have tried on android far. have ideas on how can solve above problems?

edit: have managed prepare crashing on button, image still displays on side android, displays correctly ios

i venture guess there couple of issues here. 1 xamarin.forms.labs dependency injection handler on android 1) not checking needed rotation, 2) not checking either external storage or not handling onactivitycancelled.

the easy solution problem utilize xamarin.mobile xamarin.mobile cannot 100% confirm handle if quick , easy solution.

the more hard alternative give more command roll own platform specific implementation. not going go how di works either know or can see accessing native features

here illustration of android take image code figuring out if storage external , whether or not rotation needed.

protected override void onactivityresult(int requestcode, result resultcode, intent data) { base.onactivityresult(requestcode, resultcode, data); //finishactivity(requestcode); seek { if (resultcode == result.ok) { switch (requestcode) { case take_photo: { java.io.file photo = null; if (ismounted) { photo = new java.io.file(android.os.environment.externalstoragedirectory.tostring(), sharedlibrary.strphotolocation); } else { photo = new java.io.file(cachedir, sharedlibrary.strphotolocation); } bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; options.insamplesize = 4; options.inpurgeable = true; options.ininputshareable = true; seek { //cleanup code... removed in favor of using options.injustdecodebounds info bitmap //instead of creating twice in memory //bitmap imagebitmap = bitmapfactory.decodefile(photo.absolutepath, options); //int w = imagebitmap.width; //int h = imagebitmap.height; bitmapfactory.decodefile(photo.absolutepath, options); int w = options.outwidth; int h = options.outheight; matrix matrix = new matrix(); matrix.setrotate(getneededrotation(photo.absolutepath)); options.injustdecodebounds = false; //bitmap imagebitmap = bitmap.createbitmap(bitmapfactory.decodefile(photo.absolutepath, options), 0, 0, w, h, matrix, false); bitmap imagebitmap = bitmap.createscaledbitmap(bitmapfactory.decodefile(photo.absolutepath, options), w, h, false); //...

get mounted

private system.boolean ismounted { { homecoming (system.boolean)android.os.environment.externalstoragestate.equals(android.os.environment.mediamounted); } }

getrotationneeded

private int getneededrotation(string filepath) { exifinterface exif = new exifinterface(filepath); int orientation = exif.getattributeint(exifinterface.tagorientation, -1); int rotate = 0; switch (orientation) { case 6: { rotate = 90; break; } case 3: { rotate = 180; break; } case 8: { rotate = 270; break; } default: { rotate = 0; break; } } exif.dispose(); homecoming rotate; }

android camera xamarin.forms

No comments:

Post a Comment