码迷,mamicode.com
首页 > 移动开发 > 详细

u3d 调用android相机和相册裁剪成圆形

时间:2018-02-12 13:43:20      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:lock   throw   流程   todo   return   mat   creat   rom   radius   

第一步 写方法调用相机相册

现在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);
			}        
	}

  如果你的手机是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 == 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);
	    }  

  把图片处理成圆形(如果不需要可以忽略这一步)

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();
		/* 去锯齿 */
		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");
		}

  

u3d 调用android相机和相册裁剪成圆形

标签:lock   throw   流程   todo   return   mat   creat   rom   radius   

原文地址:https://www.cnblogs.com/zhushuangmei/p/8443912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!