最是人间留不住,朱颜辞镜花辞树。这篇文章主要讲述u3d 调用android相机和相册裁剪成圆形相关的知识,希望能为你提供帮助。
第一步 写方法调用相机相册
现在u3d中写好调用相机和相册的方法(调用的同一个方法,“xj”这个是用来区分调用的类型的,path是你要存放的路径)
path =
Application.persistentDataPath
Application.persistentDataPath这个路径是用来存放一些临时数据的
文章图片
第二步 android 调用相机
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(), "头像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); } }
【u3d 调用android相机和相册裁剪成圆形】如果你的手机是android 7.0 以上的就必须这么处理。(这个自行百度,处理android7.0权限)
第三步 处理拍照完成
拍照完成后会调用这个方法
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == NONE) return; if (requestCode == PHOTOHRAPH) { Log.i("照片流程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 =https://www.songbingjia.com/android/= 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 block e.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); }
把图片处理成圆形(如果不需要可以忽略这一步)
publicBitmap 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(); /* 去锯齿 */ paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); // 保证是方形,并且从中心画 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); // 圆形,所有只用一个 int radius = (int) (Math.sqrt(w * w * 2.0d) / 2); canvas.drawRoundRect(rectF, radius, radius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
保存图片并返回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 (FileNotFoundException e) { e.printStackTrace(); }bitmap.compress(Bitmap.CompressFormat.PNG, 50, 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"); }
推荐阅读
- Android 使用DrawerLayout高速实现側滑菜单
- Framework7学习笔记之初始化App
- AndroidMainfest.xml具体解释——&lt;activity&gt;
- 今日头条移动app广告激活数据API对接完整Java代码实现供大家参考》》》项目随记
- Add Languages to Your Xamarin Apps with Multilingual App Toolkit
- 计算机科学-CSAPP-2.1 信息存储
- CSAPP实验之Data Lab
- Spring(利用ApplicationContextAware装配Bean)
- callapplybind不同使用场景