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

Posted 无止境!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
			}        
	}

  如果你的手机是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相机和相册裁剪成圆形的主要内容,如果未能解决你的问题,请参考以下文章

Android中通过访问本地相册或者相机设置用户头像

Android--利用相机或相册截取用户头像(解决了miui无法截取,以及部分机型拍照无返回Uri)

调用系统的相机和图库,并裁剪

Android调用系统相机和相册

带有 UIImagePickerController iOS 的圆形裁剪器相机?

android实现头像更改并保存(调用系统相册,系统相机)