2017-03-19 11 views
1

私は自分のアプリケーションで作業しています。カメラストリームを開き、ユーザーがボタンを押すと写真を撮るべきです、これは私のaxmlファイルです。Xamarin Androidサーフェイステクスチャリスナの設定方法

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
    <TextureView 
     android:id="@+id/textureView" 
     android:layout_marginTop="-95dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <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> 

そして、これは私のコードです:

using System; 

using Android.App; 
using Android.OS; 
using Android.Views; 
using Android.Widget; 
using Android.Hardware; 
using Android.Graphics; 

namespace CameraStream 
{ 
    [Activity(Label = "CameraStream", MainLauncher = true, Icon = "@drawable/icon")] 
    public class Activity1 : Activity, TextureView.ISurfaceTextureListener 
    { 
     Android.Hardware.Camera _camera; 
     TextureView _textureView; 

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

      _textureView = new TextureView(this); 
      _textureView.SurfaceTextureListener = this; 

      SetContentView(_textureView); 
     } 

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

      var previewSize = _camera.GetParameters().PreviewSize; 
      _textureView.LayoutParameters = 
       new FrameLayout.LayoutParams(h, w, GravityFlags.Center); 
      try 
      { 
       _camera.SetPreviewTexture(surface); 
       _camera.StartPreview(); 
      } 
      catch (Java.IO.IOException ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
      _textureView.Rotation = 90.0f; 

     } 

     public bool OnSurfaceTextureDestroyed(Android.Graphics.SurfaceTexture surface) 
     { 
      _camera.StopPreview(); 
      _camera.Release(); 

      return true; 
     } 

     public void OnSurfaceTextureSizeChanged(Android.Graphics.SurfaceTexture surface, int width, int height) 
     { 
      // camera takes care of this 
     } 

     public void OnSurfaceTextureUpdated(Android.Graphics.SurfaceTexture surface) 
     { 

     } 
    } 
} 

あなたが気づくことができるよう、それはどのように私はカメラを開き、私のボタンでストリームを開始し、コードをマージすることができ、メインAXMLを示して文句を言いませんか? 私はこのしようと試み:

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

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

      SetContentView(Resource.Layout.Main); 
     } 

をしかし、例外メッセージが現れ、私はこの質問の答えに必要なのと同様のコードが見つかりました:Xamarin Android Display a stream from the camera

をそこボタンが起動し、ストリームを終了ストリームを常に動作させたいのですが、どうしたらいいですか?おかげさまで

答えて

0

あなたはアクセス(FindViewById)その中の任意の要素の前&のinflate(SetContentView)レイアウトをロードする必要があります。

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

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

あなたがマニフェストに設定Camera権限を持っていると仮定すると、あなたが今、あなたのTextureViewにライブバックカメラのストリームを持っている必要があります。

関連する問題