Android development copy Sweep to realize photo function in shot frame _android

Source: Internet
Author: User
Tags stub

Is modeled after the sweep of the present form, surrounded by translucent occlusion, and then the middle is fully transparent, after the capture of only the contents of the box

Check a lot of blog, realize really too complex, I am more afraid of trouble so in many places lazy

First up Effect chart:

Step one: Set up camera previews and take photos

This is the premise of all steps, no preview, how users know what they have taken. The preview is with Surfaceview.

This blog has been written in great detail, open the camera, and then take pictures, and very concise! Do not want to other blog about hundreds of lines of code unintelligible. This code can be copied down when the camera template is used.

Here encountered a problem, that is, the preview effect is left 90 degrees, shot out is also left 90 degrees, check a lot of information, we unified approach is. Set the right 90 degrees in the camera preview, take the photo and then turn right 90 degrees, the specific way I will paste code

Step Two: Set semitransparent borders

A lot of blogging is about drawing or something, too responsible.

In fact, if you consider a simple point, as long as the surfaceview overlay a layer of pictures on the good, with Framelayout layout

Is the background translucent, the middle button off

Step three: How to take pictures in the frame only

This problem, many blogs are long-winded, what matrix operations are used, although very strong, but do not want to look down.

Take the picture in the frame, just shoot the whole picture, then pull the picture inside.

Bitmap.createbitmap (bitmap,50,250,650,500);

Figure out where the transparent frame is, what the coordinates are in the upper-left corner, and what the width and height are. You can use the above function to pull the image in the original photo.

Fourth step: Improve a little, add click Focus

Actually, it's just a matter of calling the focus function in the Click event.

The total code:

public class Mainactivity extends Appcompatactivity {private Surfaceview surfaceview; private Camera Camera; private But
ton take;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title);
No headers must find the call Setcontentview (R.layout.activity_main) before setting the layout; /* GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,//Set full screen display WindowManager.LayoutParams.FLAG_
fullscreen);
* * take = (Button) Findviewbyid (R.id.take);
Surfaceview = (Surfaceview) Findviewbyid (R.id.surfaceview);
Surfaceholder holder = Surfaceview.getholder ();
Holder.setfixedsize (176, 155);//Set Resolution Holder.setkeepscreenon (TRUE);
Holder.settype (surfaceholder.surface_type_push_buffers);
Surfaceview the control is created only when the activity is shown to the foreground, so it is necessary to listen for the creation of the Surfaceview Holder.addcallback (New Mysurfacecallback ()); Camera button Take.setonclicklistener (new View.onclicklistener () {@Override public void OnClick (view view) {Takepicture ()}}
); //Click event @Override Public BooLean Ontouchevent (Motionevent event) {//Focus Camera.autofocus (new Camera.autofocuscallback () {@Override public void Onaut
Ofocus (Boolean B, Camera Camera) {camera.cancelautofocus ();}});
Return Super.ontouchevent (event); /** * Monitor Surfaceview creation * @author Administrator * Surfaceview The space will be created only when the activity is displayed to the foreground * * Private Final class Mysurfaceca Llback implements Surfaceholder.callback {@Override public void surfacechanged (surfaceholder holder, int format, int Widt  h, int height) {//Todo auto-generated method stub} @Override public void surfacecreated (Surfaceholder holder) {//TODO
Auto-generated method Stub try {//When Surfaceview is created, open the camera camera = Camera.open ();
Camera.parameters params = Camera.getparameters ();
LOG.I ("I", Params.flatten ()); Params.setjpegquality (80);
Set the quality of the photo params.setpicturesize (1024, 768); Params.setpreviewframerate (5); Preview frame rate camera.setparameters (params);
Set the parameter to the camera//right 90 degrees, will preview the adjustment camera.setdisplayorientation (90); Set preview Display Camera.setpreviewdisplay (surfacevieW.getholder ());
Open preview Camera.startpreview ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();}} @Override public void surfacedestroyed (Surfaceholder holder) {//TODO auto-generated Method stub if (camera!= null) {came
Ra.release ();
camera = null; The function public void takepicture () {/* * shutter: shutter is pressed * raw: Camera captured raw data * JPEG: Camera processed data * * * camera.takepicture (NULL, NU
ll, New Mypicturecallback ()); //byte to Bitmap public Bitmap Bytes2bimap (byte[] b) {if (b.length!= 0) {return Bitmapfactory.decodebytearray (b, 0, B.le
Ngth);
else {return null;}} Bitmap turn byte public byte[] Bitmap2bytes (Bitmap BM) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); bm.compre
SS (Bitmap.CompressFormat.PNG, BAOs);
return Baos.tobytearray (); //Photo callback function, which is in fact the private final class Mypicturecallback implements Camera.picturecallback {@Override public void onpict for processing photos Uretaken (byte[] data, Camera Camera) {//TODO auto-generated Method stub try {Bitmap Bitmap = BYtes2bimap (data); Matrix M = new Matrix (); int width = bitmap.getwidth (); int height = bitmap.getheight ();
M.setrotate (90);
Rotate the photo 90 degrees bitmap = bitmap.createbitmap (bitmap, 0, 0, width, height, m, true);
LOG.D ("TAG", "width" +width);
LOG.D ("TAG", "height" +height);
Capture transparent frame Photo bitmap = Bitmap.createbitmap (bitmap,50,250,650,500);
data = Bitmap2bytes (bitmap);
File File = new file (Environment.getexternalstoragedirectory (), System.currenttimemillis () + ". jpg");
FileOutputStream fos = new FileOutputStream (file);
Fos.write (data);
The camera is occupied at the time of taking the picture, and a preview of Camera.startpreview () is required after taking the picture;
catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();}}  }
}

The above is a small set of Android to introduce the development of a sweeping sweep to achieve the picture in the frame of the photo function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.