我一直在寻找如何做到这一点。我可以访问我的相机和图库,将图像上传到我的数据库。但这就是我想要做的,我想在我的活动中拍摄照片,裁剪并在同一活动中添加文字,并在同一活动中以缩略图显示照片。 (类似于WhatsApp)。
许多教程都显示了imageview和textview,但没有在同一个活动上显示图像。 我如何实现这一目标?
答案 0 :(得分:1)
许多教程都显示了imageview和textview,但没有在同一个活动上显示图像。我如何实现这一目标?
您可以将预览放在活动的布局中,并在拍照后压缩并显示图像(步骤如下):
为相机预览创建预览类:
public class Preview : SurfaceView, ISurfaceHolderCallback
{
private Android.Hardware.Camera mCamera;
public Preview(Context context, Android.Hardware.Camera camera):base(context)
{
mCamera = camera;
Holder.AddCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
Holder.SetType(SurfaceType.PushBuffers);
}
public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
{
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (Holder.Surface == null)
{
// preview surface does not exist
return;
}
// stop preview before making changes
try
{
mCamera.StopPreview();
}
catch (Exception e)
{
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try
{
mCamera.SetPreviewDisplay(Holder);
mCamera.StartPreview();
}
catch (Exception e)
{
//Deal with exception
}
}
public void SurfaceCreated(ISurfaceHolder holder)
{
// The Surface has been created, now tell the camera where to draw the preview.
try
{
mCamera.SetPreviewDisplay(Holder);
mCamera.StartPreview();
}
catch (IOException e)
{
e.PrintStackTrace();
}
}
public void SurfaceDestroyed(ISurfaceHolder holder)
{
// empty. Take care of releasing the Camera preview in your activity.
}
}
准备布局以保存相机预览和图像:
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
<ImageView
android:id="@+id/imgResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Me"/>
</LinearLayout>
在活动中创建Camera
和Preview
并注册拍照回调:
public class MainActivity : Activity,IPictureCallback
{
bool isCameraOpened;
Preview mPreview;
Android.Hardware.Camera mCamera;
Button btnClick;
FrameLayout camera_preview;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
btnClick = FindViewById<Button>(Resource.Id.btnClick);
camera_preview = FindViewById<FrameLayout>(Resource.Id.camera_preview);
mCamera = GetCameraInstance();
mCamera.SetDisplayOrientation(90);
mPreview = new Preview(this, mCamera);
camera_preview.AddView(mPreview);
btnClick.Click += BtnClick_Click;
}
private bool CheckCameraHardware()
{
if (this.PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureCamera))
{
return true;
} else
{
return false;
}
}
private void BtnClick_Click(object sender, System.EventArgs e)
{
mCamera.TakePicture(null, null, this);
}
private Android.Hardware.Camera GetCameraInstance()
{
Android.Hardware.Camera c=null;
try
{
c = Android.Hardware.Camera.Open();
}
catch (Java.Lang.Exception e)
{
}
return c;
}
public void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
{
Bitmap bitmap=BitmapFactory.DecodeByteArray(data, 0, data.Length);
Bitmap resizedBitmap=Bitmap.CreateScaledBitmap(bitmap, 100, 100, true);
FindViewById<ImageView>(Resource.Id.imgResult).SetImageBitmap(resizedBitmap);
//restart the preview
if (mCamera != null)
{
mCamera.StartPreview();
}
}
}
有关Camera API的详细使用,请参阅Camera API。
注意:不要忘记添加<uses-permission android:name="android.permission.CAMERA" />
和
<uses-feature android:name="android.hardware.camera" />
至AndroidManifest.xml
。
答案 1 :(得分:0)
为什么不使用HashMap存储&#39;文本&#39;作为关键&amp; &#39;图片路径&#39;作为它的价值。在Application类上创建它的实例。
您可以在onActivityResult()方法上使用该HashMap。