u3d 調用android相機和相簿裁剪成圓形

來源:互聯網
上載者:User

標籤: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 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);    }  

  把圖片處理成圓形(如果不需要可以忽略這一步)

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相機和相簿裁剪成圓形

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.