2017-04-26 13 views
0

그래서 카메라 피드를 사용하여 로그인 활동을 백그라운드로 만들려고합니다. https://developer.xamarin.com/guides/android/user_interface/textureview/에있는 예제를 테스트 해 보았습니다.하지만이 작업을 수행해야합니다.Xamarin - 배경으로 카메라 피드 설정

protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     // Create your application here 
     SetContentView(Resource.Layout.Login); 

     _textureView = this.FindViewById<TextureView>(Resource.Id.textureView); 
     _textureView.SurfaceTextureListener = this; 
    } 

public void OnSurfaceTextureAvailable(
     Android.Graphics.SurfaceTexture surface, int w, int h) 
     { 
      _cam = Camera.Open(); 

      _textureView.LayoutParameters = 
        new FrameLayout.LayoutParams(w, h); 

      try 
      { 
       _cam.SetPreviewTexture(surface); 
       _cam.StartPreview(); 

      } 
      catch (Java.IO.IOException ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
     } 

axml 파일이 다소 다음과 같습니다 :

<?xml version="1.0" encoding="utf-8"?> 
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextureView 
     android:id="@+id/textureView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     AbsoluteLayout.LayoutBounds="0,0,1,1" 
     AbsoluteLayout.LayoutFlags="SizeProportional" /> 
    <LinearLayout 
     android:id="@+id/linearLayout" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/transparent" 
     AbsoluteLayout.LayoutBounds="0,0,1,1" 
     AbsoluteLayout.LayoutFlags="SizeProportional"> 

     <!-- Some other controls --> 
    </LinearLayout> 
</AbsoluteLayout> 

그러나 앱이 현재 처리되지 않은 예외를주는 OnSurfaceTextureAvailable 블록을 종료 한 후 충돌 그래서,이 시도. 예외를 깨고 작동하지 않습니다, 그것은 분명히 더 이상 실행되지 않는 스레드에 있습니다. 누군가가 왜 충돌하는지, 더 중요한 것은 그것을 고치는 방법을 알고 있습니까?

답변

0

문제는이 코드입니다 :

_textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h); 

내가 XML에서 TextureView의 레이아웃 매개 변수를 재설정하는 당신의 필요 당신이 부모를 일치하도록 설정 한 이유는 확실하지 않다. 여기 부모가 AbsoluteLayout이 아니라 FrameLayout이 아니기 때문에이 오류가 발생합니다.

이 문제를 해결하려면 AbsoluteLayout이 현재 사용되지 않으므로 XML에서 루트 컨테이너로 RelativeLayout을 사용하는 것이 좋습니다. 그런 다음 다음과 같이 코딩 할 수 있습니다 : 물론

_textureView.LayoutParameters = new RelativeLayout.LayoutParams(width, height); 

당신은 또한 XML에 FrameLayout-AbsoluteLayout을 변경할 수 있습니다, 다음 코드 _textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h); 더 이상 수정할 필요가 없습니다.

+0

아, 간단하게 그 라인을 꺼내서 오류를 수정했고, 지금 의도 한대로 카메라 피드를 보여주고 있습니다. 많은 감사드립니다. 지금은 다른 문제가 있지만이 오류와 관련이 없으므로 다른 질문을하겠습니다. –