非淡泊无以明志,非宁静无以致远。这篇文章主要讲述如何在没有预览的情况下拍摄照片。 Android系统。 Xamarin相关的知识,希望能为你提供帮助。
如何在没有预览的情况下单击按钮拍摄照片?我是Xamarin的新手,所以如果我能获得尽可能多的细节,它会很棒。
答案在.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>
【如何在没有预览的情况下拍摄照片。 Android系统。 Xamarin】在项目中选择一种方式(.axml文件或c#代码)if you run it on Android M and above, please use runtime permission, or you will get permission denied
推荐阅读
- Android键盘在Page Renderer中失去了对触摸的关注
- 当后退按钮点击两次时如何让Xamarin.Android app退出(确认包含点击之间的消息)()
- 在Android上,我在哪里存储将在卸载应用时自行删除的文件
- 如何将ImageView放在Xamarin.Android应用程序中,根据设备屏幕大小自行调整大小()
- Kotlin内联函数和Android方法都很重要
- 添加编译'com.google.android.gms(play-services-ads:11.6.0'ERROR)
- 如何在android Firestore上实现Paginate查询[重复]
- lapply和do.call有什么区别()
- Android DatePicker显示月份名称