Xây dựng ứng dụng chụp hình:
1. AndroidManifest.xml:
<uses-feature android:name="android.hardware.camera2"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2. activity_main.xml:
3. MainActivity.java:
Download sourcode demo link:
1. AndroidManifest.xml:
<uses-feature android:name="android.hardware.camera2"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2. activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="camerademo.hkh.com.camera_demo.MainActivity" android:orientation="vertical"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="89dp"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take a picture" android:id="@+id/btnCapturePicture" android:layout_gravity="center_horizontal" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imgPreview" android:visibility="gone"/> </LinearLayout> </LinearLayout>
3. MainActivity.java:
package camerademo.hkh.com.camera_demo; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100; public static final int MEDIA_TYPE_IMAGE = 1; private static final String IMAGE_DIRECTORY_NAME = "CameraImage"; private Uri fileUri; //store image private ImageView imgPreview; private Button btnCapturePicture; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imgPreview = (ImageView) findViewById(R.id.imgPreview); btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture); btnCapturePicture.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnCapturePicture: CaptureImage(); break; } } private void CaptureImage() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); } public Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); } public File getOutputMediaFile (int type) { File mediaStorageDir = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME); if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdir()) { Log.i(IMAGE_DIRECTORY_NAME, "create failed !!!!"); return null; } } String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); File mediaFile; if (type == MEDIA_TYPE_IMAGE) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); } else { return null; } return mediaFile; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { if (resultCode == RESULT_OK) { previewCapturedImage(); } else if (resultCode == RESULT_CANCELED) { Toast.makeText(getApplicationContext(), "User cancelled image capture", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Failed to capture image", Toast.LENGTH_SHORT).show(); } } } private void previewCapturedImage() { try { imgPreview.setVisibility(View.VISIBLE); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 1; final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options); imgPreview.setImageBitmap(bitmap); } catch (NullPointerException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Error ---------", Toast.LENGTH_SHORT).show(); } } }
Download sourcode demo link: