Android camera application, android camera
Preface
Android adopts the mashup design concept when designing the architecture. That is to say, everything is set up, components are written by yourself, and components are provided by others, they can be considered as their own components as long as they comply with the relevant protocols. For example, cameras and address books provided by the system can be used directly. Let's take a look at the camera content today.
Simple camera call
If we only need to call the camera directly without any subsequent processing, this is very simple. By passing an intent object, we can directly call the camera component, as shown below:
Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); mContext.startActivity(intent);
In this way, we can call out the camera components and use the camera normally.
Get the camera content
In the previous section, we used a camera and stored the captured content in the default path. However, in more cases, we not only need to call the camera to take photos, at the same time, follow-up processing is also required,
For example, if you want to place the content you have taken in the specified content area, this requirement will be a little more complicated. When you call a camera, you need to start a camera Activity with a returned value,
Then process the returned photos in the receiving interface, as shown below:
Call Code
Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); activity.startActivityForResult(intent,1);
Through the above Code, a camera Activity with a returned value is started. After the camera is taken, the return interface is called. We can process the photo content in this interface. As shown below
Return code
Protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); switch (requestCode) {case 1: // get the photo Bundle extras = data. getExtras (); Bitmap bitmap = (Bitmap) extras. get ("data"); break; default: break ;}}
Through the above code, we can get the content captured by the camera. It is a Bitmap object, and then we can use this object, when using this object, we found that this photo is not an original image, but a compressed image. If it can meet our needs, it will be OK. If we need to get the original photo, you need to use the following content.
Get original photo
If you want to obtain the original image, you need to further set it when passing the intent object. You need to save the specified original image to the specified path, as shown below:
File file = new File ("/sdcard/demo/temp"); if (! File. exists () {file. mkdirs ();} File imgFile = new File (tempDir, "test"); Intent intent = new Intent (android. provider. mediaStore. ACTION_IMAGE_CAPTURE); Uri u = Uri. fromFile (imgFile); // specifies the intent in the path. putExtra (MediaStore. EXTRA_OUTPUT, u );
In this way, the call code is written, and the camera will output the original photo to the/sdcard/demo/temp/test file.
Use original photo
This is simple. We can access and process the path in various ways. The following is a method.
Protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); switch (requestCode) {case 1: // get the photo Uri uri = data. getData (); // obtain the image path ContentResolver cr = this. getContentResolver (); Bitmap bitmap = null; try {bitmap = BitmapFactory. decodeStream (cr. openInputStream (uri);} catch (FileNotFoundException e) {e. printStackTrace ();} break; default: break ;}
In this way, we have obtained this bitmap, which will be used at will in the future.
Postscript
This article briefly introduces how to use the camera. Of course, you can also customize the camera interface. So on, I believe there is a rough outline for the simple introduction in this article.
To what extent it is used, you need to check the specific requirements and then further customize it based on the api.
Uri: The Uniform Resource Identifier (or URI) is a string used to identify an Internet Resource Name. It can identify various types of resources, such as common http resources and ftp resources. In this article, it indicates a path resource.
Address: http://www.cnblogs.com/luoaz/p/4148851.html