The road to Android development and learning-Experience of Camera

Source: Internet
Author: User

The road to Android development and learning-Experience of Camera

As the name suggests, Camera is a function for taking pictures and recording videos. If you want to take a photo and upload it, you can use camera to take a photo, store the photo, and send it to your friends. In this case, the app is not directly implemented through the camera api, because the system generally has the camera program, it is not very convenient to directly call the camera app to implement the camera function, here we will learn. In fact, camera calls the v4l2 interface at the bottom of android. For v4l2, there will be a chance to study the camera framework of android in the future.

You need to use intent to call the camera that comes with the system. You can use MediaStore to obtain the photo path. Next, try the new project CameraPictureTest and add the following code to layout:

 

     
  
  
 
 

Write the following code:

 

 

package com.example.jared.camerapicturetest;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.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;public class MainActivity extends AppCompatActivity {    public static final int TAKE_PHOTO = 1;    public static final int CROP_PICTURE = 2;    private Button takePhoto;    private ImageView picture;    private Uri imageUri;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        takePhoto = (Button)findViewById(R.id.take_photo);        takePhoto.setOnClickListener(new myOnClickListener());        picture = (ImageView)findViewById(R.id.picture);        picture.setOnClickListener(new myOnClickListener());    }    private class myOnClickListener implements View.OnClickListener {        @Override        public void onClick(View view) {            switch (view.getId()) {                case R.id.take_photo:                    setTakePhoto();                    break;                default:                    break;            }        }    }    public void setTakePhoto() {        File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");        try {            if(outputImage.exists()) {                outputImage.delete();            }            outputImage.createNewFile();        } catch (IOException e) {            e.printStackTrace();        }        imageUri = Uri.fromFile(outputImage);        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);        startActivityForResult(intent, TAKE_PHOTO);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {            case TAKE_PHOTO:                if(resultCode == RESULT_OK) {                    Intent intent1 = new Intent("com.android.camera.action.CROP");                    intent1.setDataAndType(imageUri, "image/*");                    intent1.putExtra("scale", true);                    intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                    startActivityForResult(intent1, CROP_PICTURE);                }                break;            case CROP_PICTURE:                if(resultCode == RESULT_OK) {                    try {                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()                                .openInputStream(imageUri));                        picture.setImageBitmap(bitmap);                    } catch (FileNotFoundException e) {                        e.printStackTrace();                    }                }                break;            default:                break;        }    }}

In this case, test.jpg is saved in the root directory, and the Uri of the path is passed in through intent. The camera is opened to take the photo. Here there is a response to the photo. If the returned result is successful, the CROP function is called to CROP the photo, after the image is removed, the image is displayed in the ImageView created by layout.

 

For more information, see the following link. It is implemented through a chrome Vysor plug-in and requires android 5.0 or later versions.

Now, let's take a look at the effect:

The results are basically good plug-ins. Many of them are not directly sent by taking photos, and they are also sent by selecting photos from the album. Then we can implement this function. First, layout adds the choosephoto:

 

     
      
 


Then modify the MainActivity Code as follows:

 

 

package com.example.jared.camerapicturetest;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.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;public class MainActivity extends AppCompatActivity {    public static final int TAKE_PHOTO = 1;    public static final int CROP_PICTURE = 2;    private Button takePhoto;    private Button choosePhoto;    private ImageView picture;    private Uri imageUri;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        takePhoto = (Button)findViewById(R.id.take_photo);        takePhoto.setOnClickListener(new myOnClickListener());        choosePhoto = (Button)findViewById(R.id.choose_photo);        choosePhoto.setOnClickListener(new myOnClickListener());        picture = (ImageView)findViewById(R.id.picture);        picture.setOnClickListener(new myOnClickListener());    }    private class myOnClickListener implements View.OnClickListener {        @Override        public void onClick(View view) {            switch (view.getId()) {                case R.id.take_photo:                    setTakePhoto();                    break;                case R.id.choose_photo:                    setChoosePhoto();                default:                    break;            }        }    }    public void setChoosePhoto() {        File outputImage1 = new File(Environment.getExternalStorageDirectory(), "test1.jpg");        try {            if(outputImage1.exists()) {                outputImage1.delete();            }            outputImage1.createNewFile();        } catch (IOException e) {            e.printStackTrace();        }        imageUri = Uri.fromFile(outputImage1);        Intent intent1 = new Intent("android.intent.action.GET_CONTENT");        intent1.setType("image/*");        intent1.putExtra("crop", true);        intent1.putExtra("scale", true);        intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);        startActivityForResult(intent1, CROP_PICTURE);    }    public void setTakePhoto() {        File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");        try {            if(outputImage.exists()) {                outputImage.delete();            }            outputImage.createNewFile();        } catch (IOException e) {            e.printStackTrace();        }        imageUri = Uri.fromFile(outputImage);        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);        startActivityForResult(intent, TAKE_PHOTO);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {            case TAKE_PHOTO:                if(resultCode == RESULT_OK) {                    Intent intent1 = new Intent("com.android.camera.action.CROP");                    intent1.setDataAndType(imageUri, "image/*");                    intent1.putExtra("scale", true);                    intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                    startActivityForResult(intent1, CROP_PICTURE);                }                break;            case CROP_PICTURE:                if(resultCode == RESULT_OK) {                    try {                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()                                .openInputStream(imageUri));                        picture.setImageBitmap(bitmap);                    } catch (FileNotFoundException e) {                        e.printStackTrace();                    }                }                break;            default:                break;        }    }}

It's basically the same as taking a photo. Then let's run it and see the effect:

 

Click the select photo button to go to the photo album app, select a photo, crop it, and save it, as shown in.

 

 

Appendix: see the first line of code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.