圖片剪裁 --高清圖壓縮 -- Base64上傳服務器

圖片剪裁 --高清圖壓縮 – Base64上傳服務器

應用場景:

1、webview界面與native的js交互中調用更改頭像

2、native的發佈文章動態的添加多圖

此處我的應用場景主要爲一場景,用於js交互使用,但是我調用native的剪裁功能後發現剪裁圖片時如果取消,無法獲取取消的回調操作,而且還存在很多機型的適配和系統剪裁的方形或者圓形的適配

(比如:小米的data返回爲null;華爲手機默認剪裁爲圓形,其他手機是方形;大圖高清圖壓縮失敗等等)

爲了更好的適配各類其他系統和機型,我最終妥協了,選擇一個三方的比較好的組件來完成這部分需求,你也可以借鑑下去完善它。

步驟:

一、Dialog或者PopupWindow來控制彈出拍照或相冊
二、拍照或相冊的操作後會有回調,此處我引入三方jar包完成後續操作
參考的github地址:https://github.com/crazycodeboy/TakePhoto
 compile 'com.jph.takephoto:takephoto_library:4.1.0'
三、Base64位的數據處理並調用js來實現頁面刷新和完成頭像的加載
參考的文章地址:https://blog.csdn.net/fan7983377/article/details/54410817
//注意:當前類需要是TakePhotoActivity的子類,否則無法實現下面方法
@Override
    public void takeSuccess(TResult result) {//此方法是三方包中的回調成功返回的方法
        super.takeSuccess(result);
        pushCamera(result.getImages().get(0).getCompressPath());
    }
   
    private void pushCamera(String imagePath) {
        //通過圖片路徑獲取圖片並壓縮後轉換成String
        String encoded = bitmapToString(imagePath);
        //下面這2句屬於自己js的調用,你可以改成自己需要的樣式,調用loadUrl即可
        //String str = WebLoadUrlUtils.SplicingJsContext(WebLoadUrlUtils.actionFromAppWithImageCropperSuccess, encoded);
        //WebLoadUrlUtils.loadJsUrl(WebLoadUrlUtils.actionFromAppWithImageCropperSuccess, str, webView);
    }


    //計算圖片的縮放值
    public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height/ (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    // 根據路徑獲得圖片並壓縮,返回bitmap用於顯示
    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 480, 800);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }

    //把bitmap轉換成String
    public static String bitmapToString(String filePath) {

        Bitmap bm = getSmallBitmap(filePath);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

注:引入的module中如果在裁剪時遇到裁剪框爲圓形,則修改成如下:

        if (Build.MANUFACTURER.equals("HUAWEI")) {
            intent.putExtra("aspectX", 9998);
            intent.putExtra("aspectY", 9999);
        } else {
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
        }
發佈了97 篇原創文章 · 獲贊 43 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章