U3d call Android camera and photo album cut into circles

Source: Internet
Author: User

First step Write method call camera album

Now write the method of calling camera and album in U3d (the same method called, "XJ" This is used to distinguish the type of call, path is the path you want to store)

Path = Application.persistentdatapath Application.persistentdatapath This is used to store some temporary data.

Second Step Android Call camera

public void Takephoto (String arg,string path) {path1=path; if (Arg.equals ("XJ")) {if (Build.VERSION.SDK_INT < Build.version_codes.              N) {Intent Intent = new Intent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Uri.fromfile (New File (Environment.getexternalstoragedirectory (), "              Temp.png "))); Startactivityforresult (Intent, photohraph);  } else{Intent Intent = new Intent (mediastore.action_image_capture);  File ImagePath = new file (Mcontext.getfilesdir (), "Camera_photos");   File ImagePath = new file (Environment.getexternalstoragedirectory (), "temp.png");  Uri uri = fileprovider.geturiforfile (Mcontext, "Com.elfgame.superpoker.fileprovider", ImagePath);  Headfile=imagepath;  LOG.I (Uri.tostring (), "Avatar 111111");  Intent.putextra (Mediastore.extra_output, URI);  Intent.addflags (intent.flag_grant_read_uri_permission); Startactivityforresult (Intent, photohraph);             }}else{Intent Intent = new Intent (Intent.action_pick, NULL);  Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image_unspecified);        Startactivityforresult (Intent, photozoom);} }

If your phone is Android 7.0 or higher, you have to deal with it. (This self-Baidu, processing android7.0 rights)

The third step is to finish the picture.

This method is called when the photo is completed

@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = NONE              ) return;        if (Requestcode = = photohraph) {log.i ("Photo Flow 1", "11111");        File picture = new file (Environment.getexternalstoragedirectory (), "temp.png"); if (Build.VERSION.SDK_INT < build.version_codes.        N) {startphotozoom (uri.fromfile (picture));  }else{uri uri = fileprovider.geturiforfile (Mcontext, "Com.elfgame.superpoker.fileprovider", picture);        Startphotozoom (URI);                }} if (data = = null) return;        if (Requestcode = = photozoom) {startphotozoom (Data.getdata ());            } if (Requestcode = = photoresoult) {Bundle extras = Data.getextras ();                if (extras! = null) {Bitmap photo = extras.getparcelable ("Data");        Photo =getroundedcornerbitmap (photo); ImagevieW.setimagebitmap (photo);            try {savebitmap (photo);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}    }} super.onactivityresult (Requestcode, ResultCode, data);        } public void Startphotozoom (Uri uri) {Intent Intent = new Intent ("Com.android.camera.action.CROP");               Intent.addflags (intent.flag_grant_read_uri_permission);        Intent.setdataandtype (URI, image_unspecified);        Intent.putextra ("Crop", "true");              Intent.putextra ("scale", true);        Intent.putextra ("Aspectx", 1);               Intent.putextra ("Aspecty", 1);        Intent.putextra ("Outputx", 80);        Intent.putextra ("Outputy", 80);        Intent.putextra ("Return-data", true);        LOG.E ("1111", "11111111path");    Startactivityforresult (Intent, Photoresoult);   }

Process the picture into a circle (ignore this step if you don't need to)

Public  Bitmap Getroundedcornerbitmap (Bitmap Bitmap) {if (bitmap== null) {return null;} Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (output), final paint paint = new paint ();/* antialiasing */paint.setantialias (true);p Aint.setfilterbitma P (True);p Aint.setdither (TRUE);//guaranteed to be square and drawn from the center int width = bitmap.getwidth (); int height = bitmap.getheight (); int W;int DeltaX = 0;int DeltaY = 0;if (width <= height) {w = Width;deltay = height-w;} else {w = Height;deltax = width-w;} Final rect rect = new Rect (DeltaX, DeltaY, W, W), final RECTF RECTF = new RECTF (Rect); Paint.setantialias (True); Canvas.drawargb (0, 0, 0, 0);//round, all with only one int radius = (int) (MATH.SQRT (w * w * 2.0d)/2); CANVAS.D Rawroundrect (RECTF, radius, radius, paint);p Aint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN)); Canvas.drawbitmap (Bitmap, rect, rect, paint); return output;}

Save the picture and return to U3d

public void Savebitmap (Bitmap Bitmap) throws IOException {FileOutputStream fOut = null; String path = path1; LOG.E (Path, "11111111path"); try {    file DestDir = new File (path);  if (!destdir.exists ())  {   destdir.mkdirs ();   } fOut = new FileOutputStream (path + "/" + file_name);} catch (Fil Enotfoundexception e) {e.printstacktrace ();} Bitmap.compress (Bitmap.CompressFormat.PNG, FOut), try {Fout.flush ();} catch (IOException e) {e.printstacktrace ();} try {fout.close ();} catch (IOException e) {e.printstacktrace ();} LOG.E (path + "/" + file_name, "33333333path"); Munityplayer.unitysendmessage ("Main", "Getpho", file_name); LOG.E (Path, "44444444path");}

  

U3d call Android camera and photo album cut into circles

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.