Capture Image from Camera and Display in Activity
Here's an example activity that will launch the camera app and then retrieve the image and display it.
package edu.gvsu.cis.masl.camerademo;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MyCameraActivity extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private static final int MY_CAMERA_PERMISSION_CODE = 100;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
}
else
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_PERMISSION_CODE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
else
{
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
Note that the camera app itself gives you the ability to review/retake the image, and once an image is accepted, the activity displays it.
Here is the layout that the above activity uses. It is simply a LinearLayout containing a Button with id button1 and an ImageView with id imageview1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
<ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
And one final detail, be sure to add:
<uses-feature android:name="android.hardware.camera"></uses-feature>
and if camera is optional to your app functionality. make sure to set require to false in the permission. like this
<uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>
to your manifest.xml.
Capturing Image from camera and displaying it in another activity in android
I finally found an awesome solution
This library is used to capture image from camera or select from gallery and return back image in File
format in onActivityResult
method, which can be used further in the application.
Use
EasyImage Library
Android. How to capture image from camera and save it to imageView?
just try like this:
This code for capture the image from camera and displayed in imageview.
public class MainActivity extends ActionBarActivity {
ImageView imgFavorite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void imageClick(View view){
imgFavorite =(ImageView)findViewById(R.id.imageView1);
open();
}
public void open(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //IMAGE CAPTURE CODE
startActivityForResult(intent, 0);
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
Bitmap bitmap=(Bitmap)data.getExtras().get("data");
imgFavorite.setImageBitmap(bitmap);
}
}
xml file:
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="14dp"
android:layout_marginTop="16dp"
android:contentDescription="@string/hello_world"
android:onClick="imageClick"
android:src="@drawable/camera_launcher" />
Capture Image from Camera and Display in Same Activity. -Xamarin
Lots of tutorials are displaying the imageview and textview but do not display the images on the same activity. How do i achieve this?
You can put the Preview in the layout of your activity, and compress and show the image after taking picture(Steps below):
Create a Preview class for Camera Preview:
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.
}
}Prepare the Layout to hold the Camera Preview and your Image:
<?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>Create
Camera
andPreview
in Activity and register for taking picture's callback: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();
}
}
}
For detailed use of Camera API, you can refer to Camera API.
Notes: Don't forget to add <uses-permission android:name="android.permission.CAMERA" />
and
<uses-feature android:name="android.hardware.camera" />
to AndroidManifest.xml
.
Display image in another activity after capture in android?
when the camera activity finishes, you collect the data in the onActivityResult() method.
Then you can take the file your camera created and use it in another intent and then fire off fire off another activity. That activity then reads the intent data and can manipulate the photo.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String filePath;
Bitmap _bitmapPreScale = null;
if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) {
_bitmapPreScale = getPhoto(filePath);
Intent intent = new Intent(someCLass.class)
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse ("file://" + filePath));
startActivity(intent);
}
}
Related Topics
How to Load an Imageview by Url in Android
Android Kotlin: Getting a Filenotfoundexception With Filename Chosen from File Picker
How to Change Fontfamily of Textview in Android
Basic Communication Between Two Fragments
Drawing a Line/Path on Google Maps
How to Get the Web Page Contents from a Webview
How to Prevent Android Taking a Screenshot When My App Goes to the Background
Is the Way the Firebase Database Quickstart Handles Counts Secure
Android "Only the Original Thread That Created a View Hierarchy Can Touch Its Views."
How to Enable or Disable the Gps Programmatically on Android
Fullscreen Activity in Android
Best Practice For Storing and Protecting Private API Keys in Applications
What's the Difference Between the Various Methods to Get an Android Context
How to Capture the "Virtual Keyboard Show/Hide" Event in Android