Camera component – capture image with camera

This is a code to take pictures using camera component

Code

main.xml

<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”>
<Button
android:id=”@+id/camera_bt”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:padding=”8dp”
android:text=”Button”
android:textSize=”12sp”
android:textColor=”#000000″
android:layout_gravity=”center_horizontal|center_vertical”/>
</LinearLayout>

main activitie.java

package com.my.newproject2; import android.app.*; import android.os.*; import android.view.*; import android.view.View.*; import android.widget.*; import android.content.*; import android.graphics.*; import android.media.*; import android.net.*; import android.text.*; import android.util.*; import android.webkit.*; import android.animation.*; import android.view.animation.*; import java.util.*; import java.text.*; import android.app.Activity; import android.widget.Button; import android.content.Intent; import android.net.Uri; import android.provider.MediaStore; import android.os.Build; import android.support.v4.content.FileProvider; import java.io.File; import android.view.View; import android.Manifest; import android.content.pm.PackageManager; public class MainActivity extends Activity { public final int REQ_CD_CAM = 101; private boolean connected = false; private Button camera_bt; private Intent cam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); private File _file_cam; @Override protected void onCreate(Bundle _savedInstanceState) { super.onCreate(_savedInstanceState); setContentView(R.layout.main); initialize(_savedInstanceState); if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { requestPermissions(new String[] {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000); } else { initializeLogic(); } } else { initializeLogic(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 1000) { initializeLogic(); } } private void initialize(Bundle _savedInstanceState) { camera_bt = (Button) findViewById(R.id.camera_bt); _file_cam = FileUtil.createNewPictureFile(getApplicationContext()); Uri _uri_cam = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { _uri_cam= FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".provider", _file_cam); } else { _uri_cam = Uri.fromFile(_file_cam); } cam.putExtra(MediaStore.EXTRA_OUTPUT, _uri_cam); cam.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); camera_bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View _view) { startActivityForResult(cam, REQ_CD_CAM); } }); } private void initializeLogic() { } @Override protected void onActivityResult(int _requestCode, int _resultCode, Intent _data) { super.onActivityResult(_requestCode, _resultCode, _data); switch (_requestCode) { case REQ_CD_CAM: if (_resultCode == Activity.RESULT_OK) { String _filePath = _file_cam.getAbsolutePath(); SketchwareUtil.showMessage(getApplicationContext(), _filePath); } else { SketchwareUtil.showMessage(getApplicationContext(), "Cancel"); } break; default: break; } } @Deprecated public void showMessage(String _s) { Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show(); } @Deprecated public int getLocationX(View _v) { int _location[] = new int[2]; _v.getLocationInWindow(_location); return _location[0]; } @Deprecated public int getLocationY(View _v) { int _location[] = new int[2]; _v.getLocationInWindow(_location); return _location[1]; } @Deprecated public int getRandom(int _min, int _max) { Random random = new Random(); return random.nextInt(_max - _min + 1) + _min; } @Deprecated public ArrayList<Double> getCheckedItemPositionsToArray(ListView _list) { ArrayList<Double> _result = new ArrayList<Double>(); SparseBooleanArray _arr = _list.getCheckedItemPositions(); for (int _iIdx = 0; _iIdx < _arr.size(); _iIdx++) { if (_arr.valueAt(_iIdx)) _result.add((double)_arr.keyAt(_iIdx)); } return _result; } @Deprecated public float getDip(int _input){ return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, _input, getResources().getDisplayMetrics()); } @Deprecated public int getDisplayWidthPixels(){ return getResources().getDisplayMetrics().widthPixels; } @Deprecated public int getDisplayHeightPixels(){ return getResources().getDisplayMetrics().heightPixels; } }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.