In this blog, you will get the source code for implementing camera in your android app. You just need to copy this code and use it in your app.
1. activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/container”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.Camera_Demo”>
<Button
android:layout_marginTop=”10sp”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Camera Button”
android:id=”@+id/btnCamera”
android:textColor=”#FFFFFF”
android:background=”#3F51B5″
android:textStyle=”bold” />
<ImageView
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:id=”@+id/capturedImage”
android:layout_below=”@+id/btnCamera”/>
</RelativeLayout>
2. MainActivity.java
import …
public class Camera_Demo extends AppCompatActivity {
private Button btnCamera;
private ImageView capturedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera__demo);
Typeface font = Typeface.createFromAsset( getAssets(), “fonts/TitilliumWeb-Bold.ttf” );
btnCamera = (Button) findViewById(R.id.btnCamera);
capturedImage= (ImageView) findViewById(R.id.capturedImage);
btnCamera.setTypeface(font);
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openCamera();
}
});
}
private void openCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get(“data”);
capturedImage.setImageBitmap(bp);
}
}
}
3. AndroidManifest.xml
Add Camera Permission in your manifest file:
<uses-permission android:name=”android.permission.CAMERA” />