如何点击按钮拍摄照片而不预览?我是Xamarin的新手,所以如果我能获得尽可能多的细节,那将是非常棒的。
答案 0 :(得分:1)
在.axml文件中使用它:
<SurfaceView
android:id="@+id/sfv"
android:layout_height="1dp"
android:layout_width="1dp" />
或使用它来设置c#代码中的SurfaceView
:
IWindowManager windowManager = GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
WindowManagerFlags flags = WindowManagerFlags.NotFocusable | WindowManagerFlags.NotTouchModal | WindowManagerFlags.NotTouchable;
var layoutParams = new WindowManagerLayoutParams(1, 1,
WindowManagerTypes.SystemAlert,
flags,
Format.Rgb888);
SurfaceView sfv = new SurfaceView(this);
//mll is LinearLayout
mll.AddView(sfv,layoutParams);
的活动:
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;
using System;
using Android.Content;
using Android.Runtime;
using Camera = Android.Hardware.Camera;
using Android.Graphics;
using static Android.Hardware.Camera;
using Java.IO;
namespace Cam
{
[Activity(Label = "Cam", MainLauncher = true)]
public class MainActivity : Activity, ISurfaceHolderCallback
{
private LinearLayout mll;
private Button button;
private Camera camera;
private ISurfaceHolder mHolder;
private SurfaceView sfv;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
initView();
initListener();
}
protected override void OnResume()
{
base.OnResume();
if (null == camera) {
camera = getCustomCamera();
if (mHolder != null) {
previceCamera(camera,mHolder);
}
}
}
protected override void OnStop()
{
base.OnStop();
if (null != camera) {
camera.SetPreviewCallback(null);
camera.StopPreview();
camera.Release();
camera = null;
}
}
private void previceCamera(Camera camera, ISurfaceHolder mHolder)
{
camera.SetPreviewDisplay(mHolder);
camera.SetDisplayOrientation(90);
camera.StartPreview();
}
private Camera getCustomCamera()
{
if (null == camera) {
camera = Camera.Open();
}
return camera;
}
private void initListener()
{
button.Click += picture;
}
private void picture(object sender, EventArgs e)
{
var parameters = camera.GetParameters();
parameters.PictureFormat = ImageFormatType.Jpeg;
parameters.SetPreviewSize(480,720);
parameters.FocusMode = Camera.Parameters.FocusModeAuto;
camera.AutoFocus(new MyFocusCallback(camera));
}
private void initView()
{
mll = FindViewById<LinearLayout>(Resource.Id.ll);
button = FindViewById<Button>(Resource.Id.bt);
mFloatView();
mHolder = sfv.Holder;
mHolder.AddCallback(this);
}
public void mFloatView() {
IWindowManager windowManager = GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
WindowManagerFlags flags = WindowManagerFlags.NotFocusable | WindowManagerFlags.NotTouchModal | WindowManagerFlags.NotTouchable;
var layoutParams = new WindowManagerLayoutParams(1, 1,
WindowManagerTypes.SystemAlert,
flags,
Format.Rgb888);
sfv = new SurfaceView(this);
//mll is LinearLayout
mll.AddView(sfv,layoutParams);
}
public void SurfaceCreated(ISurfaceHolder holder)
{
previceCamera(camera,holder);
}
public void SurfaceDestroyed(ISurfaceHolder holder)
{
if (null != camera) {
camera.SetPreviewCallback(null);
camera.StopPreview();
camera.Release();
camera = null;
}
}
public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
{
camera.StopPreview();
previceCamera(camera, holder);
}
}
public class MyFocusCallback :Java.Lang.Object, IAutoFocusCallback
{
Camera mCamera;
public MyFocusCallback(Camera camera){
mCamera = camera;
}
public void Dispose()
{
}
public void OnAutoFocus(bool success, Camera camera)
{
if (success) {
mCamera.TakePicture(null,null,new MyPictureCallback());
}
}
}
public class MyPictureCallback : Java.Lang.Object,IPictureCallback
{
public void Dispose()
{
throw new NotImplementedException();
}
public void OnPictureTaken(byte[] data, Camera camera)
{
dealWithCameraData(data);
}
private void dealWithCameraData(byte[] data)
{
// Here to save your photo
FileOutputStream fos = null;
string tempStr = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string fileName = tempStr + Java.Lang.JavaSystem.CurrentTimeMillis() + ".jpg";
File tempFile = new File(fileName);
fos = new FileOutputStream(fileName);
fos.Write(data);
fos.Close();
}
}
}
.axml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll">
<Button
android:id="@+id/bt"
android:text="take a photo"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<SurfaceView
android:id="@+id/sfv"
android:layout_height="1dp"
android:layout_width="1dp" />
</LinearLayout>
在项目中选择一种方式(.axml文件或c#代码)if you run it on Android M and above, please use runtime permission, or you will get permission denied