Xamarin.Android Camera on TextureView getting stretched -


i'm trying implement own camera view camera "snapchat". coming xamarin.forms i'm having hard time understand problem have. there way keep full screen , remove stretch camera view.

coming xamarin.forms can change aspect of image in xaml, couldn't find property in xamarin.android. there kind of property?

currently testing on samsung s8+.

the camera view works expected (taking pictures), view stretched image can see here enter image description here

this view code

<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="fill_parent"     android:layout_height="fill_parent">     <textureview         android:id="@+id/textureview"         android:layout_margintop="-95dp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"/>     <button         android:id="@+id/toggleflashbutton"         android:layout_width="37dp"         android:layout_height="37dp"         android:layout_gravity="top|left"         android:layout_marginleft="25dp"         android:layout_margintop="25dp"         android:background="@drawable/noflashbutton" />     <button         android:id="@+id/switchcamerabutton"         android:layout_width="35dp"         android:layout_height="26dp"         android:layout_gravity="top|right"         android:layout_marginright="25dp"         android:layout_margintop="25dp"         android:background="@drawable/togglecamerabutton" />     <button         android:id="@+id/takephotobutton"         android:layout_width="65dp"         android:layout_height="65dp"         android:layout_marginbottom="15dp"         android:layout_gravity="center|bottom"         android:background="@drawable/takephotobutton" /> </framelayout> 

and code behind

 public class camerapage : pagerenderer, textureview.isurfacetexturelistener {          #region fields          private activity activity;         private camera camera;         private camerafacing cameratype;          private bool flashon;          private byte[] imagebytes;         private surfacetexture surfacetexture;         private button switchcamerabutton;         private button takephotobutton;         private textureview textureview;         private button toggleflashbutton;         private view view;         #endregion          #region functions          public void onsurfacetextureavailable(surfacetexture surface, int width, int height) {             try {                 camera = camera.open((int)cameratype);                  textureview.layoutparameters = new framelayout.layoutparams(width, height, gravityflags.fillvertical);                 surfacetexture = surface;                 camera.setpreviewtexture(surface);                 prepareandstartcamera();             }             catch (exception ex) {                 system.diagnostics.debug.writeline(ex.message);                 system.diagnostics.debug.writeline(ex.stacktrace);             }         }          public bool onsurfacetexturedestroyed(surfacetexture surface) {             try {                 camera.stoppreview();                 camera.release();                 return true;             }             catch (exception ex) {                 system.diagnostics.debug.writeline(ex.message);                 system.diagnostics.debug.writeline(ex.stacktrace);             }             return true;         }          public void onsurfacetexturesizechanged(surfacetexture surface, int width, int height) {             prepareandstartcamera();         }          public void onsurfacetextureupdated(surfacetexture surface) {          }          protected override void onelementchanged(elementchangedeventargs<page> e) {             base.onelementchanged(e);              if (e.oldelement != null || element == null)                 return;              try {                 activity = context activity;                 view = activity.layoutinflater.inflate(resource.layout.cameralayout, this, false);                 cameratype = camerafacing.back;                  textureview = view.findviewbyid<textureview>(resource.id.textureview);                 textureview.surfacetexturelistener = this;                  takephotobutton = view.findviewbyid<button>(resource.id.takephotobutton);                 takephotobutton.click += takephotobuttontapped;                  switchcamerabutton = view.findviewbyid<button>(resource.id.switchcamerabutton);                 switchcamerabutton.click += switchcamerabuttontapped;                  toggleflashbutton = view.findviewbyid<button>(resource.id.toggleflashbutton);                 toggleflashbutton.click += toggleflashbuttontapped;                  addview(view);             }             catch (exception ex) {                 system.diagnostics.debug.writeline(ex.message);                 system.diagnostics.debug.writeline(ex.stacktrace);             }         }            protected override void onlayout(bool changed, int l, int t, int r, int b) {             try {                 base.onlayout(changed, l, t, r, b);                  var msw = measurespec.makemeasurespec(r - l, measurespecmode.atmost);                 var msh = measurespec.makemeasurespec(b - t, measurespecmode.atmost);                  view.measure(msw, msh);                 view.layout(0, 0, r - l, b - t);             }             catch (exception ex) {                 system.diagnostics.debug.writeline(ex.message);                 system.diagnostics.debug.writeline(ex.stacktrace);             }         }          private void prepareandstartcamera() {             try {                 camera.stoppreview();                  var display = activity.windowmanager.defaultdisplay;                  if (display.rotation == surfaceorientation.rotation0) camera.setdisplayorientation(90);                  if (display.rotation == surfaceorientation.rotation270) camera.setdisplayorientation(180);                  camera.startpreview();             }             catch (exception ex) {                 system.diagnostics.debug.writeline(ex.message);                 system.diagnostics.debug.writeline(ex.stacktrace);             }         }          private void switchcamerabuttontapped(object sender, eventargs e) {             try {                 if (cameratype == camerafacing.front) {                     cameratype = camerafacing.back;                      camera.stoppreview();                     camera.release();                     camera = camera.open((int)cameratype);                     camera.setpreviewtexture(surfacetexture);                     prepareandstartcamera();                 }                 else {                     cameratype = camerafacing.front;                      camera.stoppreview();                     camera.release();                     camera = camera.open((int)cameratype);                     camera.setpreviewtexture(surfacetexture);                     prepareandstartcamera();                 }             }             catch (exception ex) {                 system.diagnostics.debug.writeline(ex.message);                 system.diagnostics.debug.writeline(ex.stacktrace);             }         }          private void toggleflashbuttontapped(object sender, eventargs e) {             try {                 flashon = !flashon;                 if (flashon) {                     if (cameratype == camerafacing.back) {                         toggleflashbutton.setbackgroundresource(resource.drawable.flashbutton);                         cameratype = camerafacing.back;                          camera.stoppreview();                         camera.release();                         camera = camera.open((int)cameratype);                         var parameters = camera.getparameters();                         parameters.flashmode = camera.parameters.flashmodetorch;                         camera.setparameters(parameters);                         camera.setpreviewtexture(surfacetexture);                         prepareandstartcamera();                     }                 }                 else {                     toggleflashbutton.setbackgroundresource(resource.drawable.noflashbutton);                     camera.stoppreview();                     camera.release();                      camera = camera.open((int)cameratype);                     var parameters = camera.getparameters();                     parameters.flashmode = camera.parameters.flashmodeoff;                     camera.setparameters(parameters);                     camera.setpreviewtexture(surfacetexture);                     prepareandstartcamera();                 }             }             catch (exception ex) {                 system.diagnostics.debug.writeline(ex.message);                 system.diagnostics.debug.writeline(ex.stacktrace);             }         }          private async void takephotobuttontapped(object sender, eventargs e) {             try {                 camera.stoppreview();                 dialogservice.showloading("capturing...");                  var image = textureview.bitmap;                 using (var imagestream = new memorystream()) {                     await image.compressasync(bitmap.compressformat.png, 75, imagestream);                     image.recycle();                     imagebytes = imagestream.toarray();                 }                   camera.startpreview();                 await app.navigationcontroller.pushasync(new imagepreviewview(imagebytes));                 dialogservice.hideloading();             }             catch (exception ex) {                 system.diagnostics.debug.writeline(ex.message);                 system.diagnostics.debug.writeline(ex.stacktrace);             }         }          #endregion     } 

try aspect ratio in onmeasure:

protected void onmeasure(int widthspec, int heightspec) {//} 

check links , camera preview stretched, camera preview

try original here

protected override void onmeasure(int widthmeasurespec, int heightmeasurespec)         {             // purposely disregard child measurements because act             // wrapper surfaceview centers camera preview instead             // of stretching it.             int width = resolvesize(suggestedminimumwidth, widthmeasurespec);             int height = resolvesize(suggestedminimumheight, heightmeasurespec);             setmeasureddimension(width, height);              if (_msupportedpreviewsizes != null)             {                 _mpreviewsize = getoptimalpreviewsize(_msupportedpreviewsizes, width, height);             }         } 

check link :

camera preview stretched on few devices

hope may you.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -