Since I have just entered the current company, they are more involved in the picture, so they are going to write a series of pictures of the article, first of all, from the place of the production of the picture began to write –camera
If your app just needs to take a picture, just call the system's camera to meet your needs.
Calling the system's camera through Action_image_capture
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
Then jump through the Startactivityforresult method
Onactivityresult:
Bundledata.getExtras();Bitmap bitmap = (Bitmap) extras.get("data");showImage.setImageBitmap(bitmap);
In general, this will satisfy your needs, do not need to increase permissions, but it is important to note that the data obtained by a thumbnail, if you want to get an original image, you need to specify the picture's save address
Uri uri = Uri.fromFile(newFile(path)); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
Also use the Startactivityforresult method to jump
Onactivityresult:
fileinputstream FileInputStream = null ; Bitmap output; try {int degree = photoutil.readpicturedegree (path); FileInputStream = new fileinputstream (new File (path)); Output = Bitmapfactory.decodestream (FileInputStream); Showimage.setimagebitmap (Photoutil.rotaingimageview (Degree,output));} catch (FileNotFoundException e) {E.printstacktrace ();} finally {if (fileinputstream! = null ) {try {fileinputstream.close (); } catch (IOException e) {e.printstacktrace (); } }}
Actually, it takes two words to get this bitmap.
new FileInputStream(newFile(path));output = BitmapFactory.decodeStream(fileInputStream);
But saved in the local picture, the photo is rotated 90 degrees, the picture rotation angle is recorded in the Exif, so in order to rotate the picture, simply use EXIF to take the angle.
/** * Read Picture properties: Rotation angle * @param Path Picture Absolute path * @return degree rotation angle */ Public Static int Readpicturedegree(String Path) {intdegree =0;Try{Exifinterface Exifinterface =NewExifinterface (path);intOrientation = Exifinterface.getattributeint (exifinterface.tag_orientation, exifinterface.orientation_normal);Switch(orientation) { CaseExifInterface.ORIENTATION_ROTATE_90:degree = -; Break; CaseExifInterface.ORIENTATION_ROTATE_180:degree = the; Break; CaseExifInterface.ORIENTATION_ROTATE_270:degree = the; Break; } }Catch(IOException e) {E.printstacktrace (); }returndegree; }
Then rotate the bitmap according to the angle taken.
publicstaticrotaingImageView(int angle , Bitmap bitmap) { new Matrix(); matrix.postRotate(angle); 00true); return resizedBitmap; }
For pictures, let's do it later.
This allows you to get the original image, and to be able to display the picture in a positive direction, but many apps don't just take a photo, so here's how to start the custom camera
PackageCom.zimo.guo.customcamera.view;ImportAndroid.content.Context;ImportAndroid.content.pm.PackageManager;ImportAndroid.graphics.ImageFormat;ImportAndroid.hardware.Camera;ImportAndroid.os.Environment;ImportAndroid.util.AttributeSet;ImportAndroid.view.SurfaceHolder;ImportAndroid.view.SurfaceView;ImportJava.io.File;ImportJava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.IOException;/** * Created by Zimo on 15/12/27. * * Public class cameraview extends surfaceview implements Surfaceholder . Callback { PrivateCamera camera;PrivateSurfaceholder Holder;PrivateContext context;PrivateString Picurl; Public Cameraview(Context context) {Super(context); This. Context = Context; Initholder (); } Public Cameraview(context context, AttributeSet attrs) {Super(context, attrs); This. Context = Context; Initholder (); } Public Cameraview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); This. Context = Context; Initholder (); }Private void Initholder() {holder = This. Getholder (); Holder.addcallback ( This); }Private Boolean Existcamera(Context context) {returnContext.getpackagemanager (). Hassystemfeature (Packagemanager.feature_camera); }/** * Initialize the camera * / Private void Initcamera() {if(Camera = =NULL) {camera = Camera.open (); } }Private void Imagepreview(Surfaceholder Holder) {Try{if(Camera! =NULL) {camera.setpreviewdisplay (holder); Camera.setdisplayorientation ( -); Camera.startpreview (); } }Catch(IOException e) {E.printstacktrace (); } }Private void setparameters() {if(Camera! =NULL) {Camera.parameters Parameters = Camera.getparameters (); Parameters.setpictureformat (Imageformat.jpeg);//Parameters.setrotation (+);Parameters.setflashmode (Camera.Parameters.FLASH_MODE_AUTO); Camera.setparameters (parameters); } }Private void Releasecamera() {if(Camera! =NULL) {Camera.setpreviewcallback (NULL); Camera.stoppreview (); Camera.release (); Camera =NULL; } } Public void AutoFocus(){if(Camera! =NULL) {Camera.autofocus (NewCamera.autofocuscallback () {@Override Public void Onautofocus(BooleanSuccess, camera camera) {if(success) {takepicture (); } } }); } } Public void takepicture(){if(Camera! =NULL) {Camera.takepicture (NULL,NULL,NewCamera.picturecallback () {@Override Public void Onpicturetaken(byte[] data, camera camera) {if(Picurl = =NULL) {Picurl = Environment.getexternalstoragedirectory () + File.separator +"Zimo.jpg"; } File File =NewFile (Picurl);if(File.exists ()) {File.delete (); }Try{FileOutputStream FOS =NewFileOutputStream (file); Fos.write (data); Fos.close (); }Catch(FileNotFoundException e) {E.printstacktrace (); }Catch(IOException e) {E.printstacktrace (); } imagepreview (holder); } }); } }@Override Public void surfacecreated(Surfaceholder Holder) {if(Existcamera (context)) {Initcamera (); } }@Override Public void surfacechanged(Surfaceholder holder,intFormatintWidthintHeight) {setparameters (); Imagepreview (holder); }@Override Public void surfacedestroyed(Surfaceholder Holder) {Releasecamera (); } Public void Setpicurl(String Picurl) { This. Picurl = Picurl; }}
The above is a custom camera that inherits the Surfaceview
- Check if camera exists
- Initialize camera
- Setting parameters
- Preview picture
- Photo
- Release camera
This is the step of customizing the camera implementation and, of course, adding permissions
<uses-permission android:name="android.permission.CAMERA"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Just use Cameraview as a custom control.
<com.zimo.guo.customcamera.view.CameraView android:id="@+id/camera" android:layout_width="match_parent" android:layout_height="match_parent" />
Click the Take photos button to call Takepicture (), of course, you can also define their own image address
cameraView.setPicUrl(path);cameraView.takePicture();
You can also automatically take pictures after focusing
cameraView.autoFocus();
The next article began to study the picture, this article first to this!
Android's Camera