Android Custom Camera take a screenshot and follow nice add tag

Source: Internet
Author: User

The project recently need to use the camera to take pictures, at the beginning of the time must be how easy how to come, is directly called the system's camera, then the problem came, call the system camera, found that different mobile phone shots of the photo rotation angle is not the same. For example, the millet phone shot the picture rotation angle of 0 degrees (the photo is positive), the Samsung phone photos taken, the photos are loaded in the same way, and the photos rotated 90 degrees, (the photo is reversed), shit, this is why?

At the beginning of the picture must be rotated a certain angle to solve, but found that there is a little low ah, and the design requirements in the photo screen to draw a frame, the user see this box after taking pictures, take photos after the automatic interception of images, and compressed to 640*640,shit, So the camera that called the system must have been out of the way ...

There is no way, only to the Internet to search the relevant solutions, but the search for a long time is basically a simple preview, hey, finally to the official Google example found a better surfaceview, This surfaceview, at least, calculates the most appropriate proportion of photos based on your phone, OK. It's not enough, it's too simple. Finally through the multi-channel (refer to the previous project implementation, ask a Big brother), probably understand the fundamentals of the camera.

The following is a brief description of the camera:

1: We want to take pictures correctly, the screen reality of the custom camera interface if the horizontal screen shooting, preferably set to full screen and no title, so that the photo is correct, otherwise you will cry, it is also the blood and tears to find out, the following simple paste a configuration file:

<activity                android:name= "Com.example.AndroidCaptureCropTags.camera.ActivityCapture"                android: Configchanges= "Keyboardhidden|orientation|screensize"                android:screenorientation= "Landscape"                android: Theme= "@android: Style/theme.notitlebar.fullscreen"                android:windowsoftinputmode= "Adjustresize|statehidden" >        </activity>

2: In fact, when we take a photo preview, Google has encapsulated a series of APIs, mainly the preview before the best calculation of the preview scale, to prevent the deformation of the preview. And calculate the best shot out of the realistic proportions of the picture, so that the picture will not be deformed.

3: Next is the camera interface white box drawing, is a custom view.

4: After the last shot of the picture, according to the white box shown in the screen to intercept the picture (this place is also very painful ...) );

Picturecallback = new Picturecallback () {@Overridepublic void Onpicturetaken (byte[] data, camera camera) {_iscapturing = f Alse; Bitmap Bitmap = null;try {bitmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = true; Bitmapfactory.decodebytearray (data, 0, Data.length, Options),//debug.debug ("width--:" + options.outwidth + "height--: "+ options.outheight); options.injustdecodebounds = False;options.inpreferredconfig = bitmap.config.argb_8888;// The picture is compressed here Options.insamplesize = Math.max (Options.outwidth/kphotomaxsavesidelen, options.outheight/ Kphotomaxsavesidelen); bitmap = Bitmaputil.decodebytearrayunthrow (data, options); if (null = = Bitmap) { Options.insamplesize = Math.max (2, Options.insamplesize * 2); bitmap = Bitmaputil.decodebytearrayunthrow (data, options) ;}} catch (Throwable e) {}if (null = = Bitmap) {Toast.maketext (activitycapture.this, "Not enough memory to save the photo failed! ", Toast.length_short). Show (); return;} Long start = System.currenttimemillis (); Bitmap AddBitmap = BitmapuTil.rotateandscale (Bitmap, _rotation, Kphotomaxsavesidelen, true); Bitmap Finalbitmap = Cropphotoimage (AddBitmap); File photofile = Pathmanager.getcropphotopath (); Boolean successful = Bitmaputil.savebitmap2file (Finalbitmap, Photofile, Bitmap.CompressFormat.JPEG, +); while (!successful) {successful = Bitmaputil.savebitmap2file (Finalbitmap , Photofile, Bitmap.CompressFormat.JPEG, 100);} if (Finalbitmap! = null &&!finalbitmap.isrecycled ()) {addbitmap.recycle ();} Intent Intent = new Intent (); Intent.putextra (Kphotopath, Photofile.getabsolutepath ()); ActivityCapture.this.setResult (RESULT_OK, intent); ActivityCapture.this.finish ();}};
The logic of the above code is: After shooting the system will be the image of the data in the form of byte[] to us, options.injustdecodebounds = true; First Use this form to avoid directly loading the picture into memory, get the width and height of the picture, And then calculate the picture of the insamplesize, here wrote one of the largest side length 1600, through the experiment to find the definition of this side length, the photos taken, whether it is on the phone or on the computer to see, the experience is good, this is by asking before the eldest brother to understand, (I think for a long while I do not know why it must be defined as 1600 ....) )。 The following is the loading of data into a bitmap, note that at this point the bitmap may be rotated, but also call this method bitmap AddBitmap = Bitmaputil.rotateandscale (Bitmap, _rotation, Kphotomaxsavesidelen, true); To put the picture in place, where the _rotation angle is our constant calculation during the filming process (some of it is from the Internet, is being digested ...). , he is not too clear to say, free to say the wrong fraught). After the picture is rotated correctly, the following is what you see and what you get, ....

Tailoring the private Bitmap cropphotoimage (Bitmap bmp) {int DW = Bmp.getwidth (), int dh = bmp.getheight (), and int height;int wid According to the pictures taken Th;if (DH > DW) {//Picture vertical//Cut the picture when you follow the vertical screen to calculate height = Getwindowmanager (). Getdefaultdisplay (). getwidth (); width = Getwindowmanager (). Getdefaultdisplay (). GetHeight ();} else {//the picture is horizontal//cut the picture when you follow the horizontal screen to calculate the width = Getwindowmanager (). Getdefaultdisplay (). getwidth (); height = Getwindowmanager (). Getdefaultdisplay (). GetHeight ();} Rect rect = new rect (), int left = (Width-cropborderview.getrect (). Width ())/2;int top = (height-cropborderview.getrec T (). Height ())/2;int right = left + Cropborderview.getrect (). width (), int bottom = top + cropborderview.getrect (). Height () ; Rect.set (left, top, right, bottom); float scale = 1.0f;//If the picture is wide or tall on the screen, zoom to the width or height of the screen if (DW > Width && dh <= h Eight) {scale = width * 1.0F/DW;} if (DH > Height && DW <= width) {scale = height * 1.0F/DH;} If the width and height are larger than the screen, let it fit the screen size proportionally if (DW > Width && dh > height) {scale = Math.max (WidtH * 1.0f/dw, Height * 1.0f/dh);} If the width and height of the picture are less than the width and height of the screen, zoom in to the screen size if (DW < width && DH < height) {scale = width * 1.0F/DW;} Matrix matrix = new Matrix (); Matrix.postscale (scale, scale); try {Bitmap b2 = bitmap.createbitmap (BMP, 0, 0, DW, DH, matrix , true); if (null! = B2 && bmp! = b2) {bmp.recycle (); bmp = B2;}} catch (OutOfMemoryError e) {e.printstacktrace ();} try {Bitmap B3 = Bitmap.createbitmap (BMP, Rect.left, Rect.top, Rect.width (), Rect.height ()); if (null! = B3 && bmp ! = B3) {bmp.recycle (); bmp = B3;}} catch (OutOfMemoryError e) {e.printstacktrace ();} Compress the picture to 640*640try {Bitmap b4 = bitmap.createscaledbitmap (BMP, 640, 640, false), if (null! = B4 && bmp! = B4) {BMP. Recycle (); bmp = B4;}} catch (OutOfMemoryError e) {e.printstacktrace ();} return bmp;}

After testing, it was found that the photos were not seen vertically or vertically in the screen, and might be taken in a vertical or horizontal direction (that is, the photo is vertical or sideways). This is a two-way situation to be dealt with separately ... The following is a simple description of both cases.

1: Photos are vertical, and our activity is horizontal screen ... So when we calculate the frame, we have to calculate the screen as vertical, so that we can determine the correct position of the Intercept box.

2: Photos are horizontal, our activity is horizontal screen ... So the normal calculation is OK.

3: Don't think about the size of our screen, the photos are the size of the screen, but much larger than the screen .... , which requires us to zoom to the size of the screen so that you can do what you see is what you get, don't you think? The specific zoom is nothing more than the scale of the picture. Picture is scaled, but may worry about this situation, if the zoom is smaller or larger than the screen is not accurate? I was worried about this at first, but we calculated the most appropriate preview scale in the code, the most appropriate proportion of photos, after a lot of mobile phone testing found that there is no problem, their proportions are consistent.

4: Next is annoying, finally compress the picture to 640*640, OK.

5: Understand a little do not repeat the building of the car, your heart is much more comfortable.

Saved to the local file is OK. The following is to add a tag to the picture or something, tag add point animation, are a little bit out of the technical content, including the square of the control of the custom is also very simple, directly look at the code is OK .....

Grass, I found that I can not write a blog so detailed ... Shit




Android custom camera to take pictures and tag with nice

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.