日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

安卓高级6 拍照或者从相册获取图片 并检测旋转角度或者更新画册扫描

發(fā)布時(shí)間:2025/3/21 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓高级6 拍照或者从相册获取图片 并检测旋转角度或者更新画册扫描 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 旋轉(zhuǎn)角度

當(dāng)我們從手機(jī)讀取圖片時(shí)候發(fā)現(xiàn)其圖片旋轉(zhuǎn)了90或者其他度數(shù),比如三星拍照后的照片就是個(gè)例子.這時(shí)候我們讀取出來(lái)判斷角度然后在逆向回轉(zhuǎn)為正向的圖片

  • 所需類(lèi) ExifInterface
    Exif是 Exchangeable Image File 縮寫(xiě)
  • /*** 讀取圖片屬性:旋轉(zhuǎn)的角度* * @param path 圖片絕對(duì)路徑* @return degree 旋轉(zhuǎn)角度*/public static int readPictureDegree(String path) {int degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);switch (orientation) {case ExifInterface.ORIENTATION_ROTATE_90:degree = 90;break;case ExifInterface.ORIENTATION_ROTATE_180:degree = 180;break;case ExifInterface.ORIENTATION_ROTATE_270:degree = 270;break;}} catch (IOException e) {e.printStackTrace();}return degree;} /*** 旋轉(zhuǎn)圖片* * @param angle 旋轉(zhuǎn)角度* @param bitmap 原圖* @return bitmap 旋轉(zhuǎn)后的圖片*/public static Bitmap rotateImage(int angle, Bitmap bitmap) {// 圖片旋轉(zhuǎn)矩陣Matrix matrix = new Matrix();matrix.postRotate(angle);// 得到旋轉(zhuǎn)后的圖片Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);return resizedBitmap;}

    2. 調(diào)用系統(tǒng)的剪切圖片軟件

    當(dāng)我們某個(gè)圖片過(guò)大 想讓用戶自行選擇剪切區(qū)域

  • 意圖Intent的action “com.android.camera.action.CROP”
  • 意圖Intent的data類(lèi)型 文件地址Uri 如Uri.fromFile(new File(fileSrc))
  • 意圖intent的type類(lèi)型(mime) “image/*”
  • 以下是可以傳入到intent的數(shù)值 (putExtra)

    數(shù)值名(字符串類(lèi)型)傳入值類(lèi)型作用案例
    cropString設(shè)置true才能出剪輯的小方框,不然沒(méi)有剪輯功能,只能選取圖片intent.putExtra(“crop”, “true”);
    aspectXint放大縮小比例的Xintent.putExtra(“aspectX”, 1);
    aspectYint放大縮小比例的Yintent.putExtra(“aspectY”, 1);
    outputXint//這個(gè)是限制輸出圖片x方向大小(最大限制)intent.putExtra(“outputX”, 320);
    outputYint//這個(gè)是限制輸出圖片y方向大小(最大限制)intent.putExtra(“outputY”, 320);
    return-databoolean是否返回?cái)?shù)據(jù)圖(后面有案例)intent.putExtra(“return-data”, true);
    scale和scaleUpIfNeededboolean切圖大小不足輸出,邊緣出現(xiàn)類(lèi)似毛邊或者鋸齒等innerIntent.putExtra(“scale”, true);innerIntent.putExtra(“scaleUpIfNeeded”, true);
    /**** 裁剪圖片* @param activity Activity* @param uri 圖片的Uri*/public static void cropPicture(Activity activity, Uri uri) {Intent innerIntent = new Intent("com.android.camera.action.CROP");innerIntent.setDataAndType(uri, "image/*");innerIntent.putExtra("crop", "true");// 設(shè)置true才能出剪輯的小方框,不然沒(méi)有剪輯功能,只能選取圖片innerIntent.putExtra("aspectX", 1); // 放大縮小比例的XinnerIntent.putExtra("aspectY", 1);// 放大縮小比例的X 這里的比例為: 1:1innerIntent.putExtra("outputX", 320); //這個(gè)是限制輸出圖片大小innerIntent.putExtra("outputY", 320); innerIntent.putExtra("return-data", true);// 切圖大小不足輸出,無(wú)黑框innerIntent.putExtra("scale", true);innerIntent.putExtra("scaleUpIfNeeded", true);File imageFile = new File(getImagePath(activity.getApplicationContext()));innerIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));innerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());activity.startActivityForResult(innerIntent, REQUEST_CROP_IMAGE);} //回調(diào):activityforresult @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data){ // 獲取返回?cái)?shù)據(jù) Bitmap bmp = data.getParcelableExtra("data");}

    3. 拍照獲取圖片

  • intent 的action: MediaStore.ACTION_IMAGE_CAPTURE
    直接案例:
  • // 設(shè)置相機(jī)拍照后照片保存路徑File mPictureFile = new File(Environment.getExternalStorageDirectory(), "picture" + System.currentTimeMillis()/1000 + ".jpg");// 啟動(dòng)拍照,并保存到臨時(shí)文件Intent mIntent = new Intent();mIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);mIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPictureFile));mIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);startActivityForResult(mIntent, REQUEST_CAMERA_IMAGE);

    回調(diào)acativityforesult

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode != RESULT_OK) {return;}if (!mPictureFile.exists()) {showTip("拍照失敗,請(qǐng)重試");return;}fileSrc = mPictureFile.getAbsolutePath();}

    4.更新畫(huà)冊(cè)

    當(dāng)你更新一張圖片在手機(jī)中時(shí)不會(huì)立即出現(xiàn)在圖冊(cè)中,需要重啟手機(jī)或者通知系統(tǒng)去掃描

    private void updateGallery(String filename) {MediaScannerConnection.scanFile(this, new String[] {filename}, null,new MediaScannerConnection.OnScanCompletedListener() {@Overridepublic void onScanCompleted(String path, Uri uri) {}});}

    5.從手機(jī)相冊(cè)獲取

    直接案例

    Intent intent = new Intent();intent.setType("image/*");intent.setAction(Intent.ACTION_PICK);startActivityForResult(intent, REQUEST_PICTURE_CHOOSE);

    回調(diào):

    @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode != RESULT_OK) {return;} if ("file".equals(data.getData().getScheme())) {// 有些低版本機(jī)型返回的Uri模式為filefileSrc = data.getData().getPath();} else {// Uri模型為content//選擇數(shù)據(jù)地址字段String[] proj = {MediaStore.Images.Media.DATA};Cursor cursor = getContentResolver().query(data.getData(), proj,null, null, null);cursor.moveToFirst();//獲取字段在第幾列int idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);//獲取所在地址fileSrc = cursor.getString(idx);cursor.close();}}

    轉(zhuǎn)載于:https://www.cnblogs.com/muyuge/p/6152133.html

    總結(jié)

    以上是生活随笔為你收集整理的安卓高级6 拍照或者从相册获取图片 并检测旋转角度或者更新画册扫描的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。