Search in sources :

Example 1 with OnCompressListener

use of top.zibin.luban.OnCompressListener in project RxTools by vondear.

the class RxCameraTool method initCameraEvent.

public static void initCameraEvent(final Context mContext, final CameraView mCameraView, final byte[] data, final String fileDir, final String picName, final double mLongitude, final double mLatitude, final boolean isEconomize, final OnRxCamera OnRxCamera) {
    OnRxCamera.onBefore();
    RxTool.getBackgroundHandler().post(new Runnable() {

        @Override
        public void run() {
            File fileParent = new File(fileDir);
            File cacheParent = new File(RxFileTool.getCecheFolder(mContext) + File.separator + "cache" + File.separator + "picture");
            if (!cacheParent.exists()) {
                cacheParent.mkdirs();
            }
            if (!fileParent.exists()) {
                fileParent.mkdirs();
            }
            final File cachefile = new File(cacheParent, picName);
            final File compressFile = new File(fileParent, picName);
            OutputStream os = null;
            try {
                os = new FileOutputStream(cachefile);
                os.write(data);
                os.close();
                Luban.with(mContext).load(cachefile).setCompressListener(new OnCompressListener() {

                    @Override
                    public void onStart() {
                        Log.d("图片压缩", "开始压缩");
                    }

                    @Override
                    public void onSuccess(File file) {
                        if (RxFileTool.copyOrMoveFile(file, compressFile, true)) {
                            Log.d("图片压缩", "压缩完成");
                            OnRxCamera.onSuccessCompress(compressFile);
                            if (mLongitude != 0 || mLatitude != 0) {
                                RxExifTool.writeLatLonIntoJpeg(compressFile.getAbsolutePath(), mLatitude, mLongitude);
                                OnRxCamera.onSuccessExif(compressFile);
                                RxToast.normal("拍照成功");
                            } else {
                                RxToast.error("请先获取定位信息");
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("图片压缩", "压缩异常");
                        Logger.d(e);
                    }
                }).launch();
            } catch (IOException e) {
                Log.w("onPictureTaken", "Cannot write to " + compressFile, e);
            } finally {
                if (isEconomize) {
                    mCameraView.stop();
                }
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                    // Ignore
                    }
                }
            }
        }
    });
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) OnCompressListener(top.zibin.luban.OnCompressListener) IOException(java.io.IOException) File(java.io.File)

Example 2 with OnCompressListener

use of top.zibin.luban.OnCompressListener in project keleFanfou by kelefun.

the class SendStatusFragmentChild method postStatus.

/**
 * 发送
 */
private void postStatus() {
    ToastUtil.showToast(_mActivity, "正在发布中...");
    // backToHome();
    StatusApi api = BaseRetrofit.retrofit(new SignInterceptor()).create(StatusApi.class);
    if (imageUrl != null) {
        // 带图片
        RequestBody status = RequestBody.create(MediaType.parse("text/plain"), editText.getText().toString());
        // TODO 如果不是gif则压缩图片
        Luban.with(_mActivity).load(imageUrl).ignoreBy(// 2MB
        1024 * 2).putGear(4).setCompressListener(new OnCompressListener() {

            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(File file) {
                RequestBody photo = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                MultipartBody.Part body = MultipartBody.Part.createFormData("photo", file.getName(), photo);
                Call<Status> call = api.uploadPhoto(status, body);
                enqueue(call);
            }

            @Override
            public void onError(Throwable e) {
            }
        }).launch();
    } else {
        // 只有文字
        if ("".equals(editText.getText().toString())) {
            ToastUtil.showToast(_mActivity, "输入不能为空");
            return;
        }
        String status = editText.getText().toString();
        Map<String, String> partMap = new ArrayMap<>();
        partMap.put("status", status);
        Call<Status> call = api.postStatus(partMap);
        enqueue(call);
    }
}
Also used : Status(com.funstill.kelefun.data.model.Status) StatusApi(com.funstill.kelefun.data.api.StatusApi) SignInterceptor(com.funstill.kelefun.http.SignInterceptor) OnCompressListener(top.zibin.luban.OnCompressListener) ArrayMap(android.support.v4.util.ArrayMap) MultipartBody(okhttp3.MultipartBody) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 3 with OnCompressListener

use of top.zibin.luban.OnCompressListener in project ttdj by soonphe.

the class PublishGoodsFragment method compressWithLs.

/**
 * Luban压缩图片 Listener 方式
 */
private void compressWithLs(final List<String> photos) {
    Luban.with(this.getContext()).load(photos).ignoreBy(100).setTargetDir(getContext().getExternalFilesDir(DIRECTORY_PICTURES).getAbsolutePath()).setCompressListener(new OnCompressListener() {

        @Override
        public void onStart() {
        }

        @Override
        public void onSuccess(File file) {
            // 将选择的文件放入集合中
            if (fileList != null) {
                fileList.add(file);
            } else {
                fileList = new ArrayList<>();
                fileList.add(file);
            }
            // compressPicList.add(file);
            LogUtils.e("当前图片信息:" + file.getPath() + "当前图片大小:" + file.length());
            LogUtils.e("要加载的图片路径:" + file.getAbsolutePath());
            gridViewData.add(new PostMessageImageItem(file.getAbsolutePath()));
            gridadapter.notifyDataSetChanged();
            if (picList.size() == compressPicList.size()) {
                // 图片上传
                LogUtils.e("______图片压缩完毕", "图片list信息:" + compressPicList.size() + "--" + picList.size());
            }
        }

        @Override
        public void onError(Throwable e) {
            LogUtils.e("______图片压缩失败" + e.toString());
        }
    }).launch();
}
Also used : PostMessageImageItem(com.connxun.ltcx.entity.PostMessageImageItem) OnCompressListener(top.zibin.luban.OnCompressListener) File(java.io.File)

Example 4 with OnCompressListener

use of top.zibin.luban.OnCompressListener in project Luban by Curzibn.

the class MainActivity method withLs.

private <T> void withLs(final List<T> photos) {
    Luban.with(this).load(photos).ignoreBy(100).setTargetDir(getPath()).setFocusAlpha(false).filter(new CompressionPredicate() {

        @Override
        public boolean apply(String path) {
            return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
        }
    }).setRenameListener(new OnRenameListener() {

        @Override
        public String rename(String filePath) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(filePath.getBytes());
                return new BigInteger(1, md.digest()).toString(32);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            return "";
        }
    }).setCompressListener(new OnCompressListener() {

        @Override
        public void onStart() {
        }

        @Override
        public void onSuccess(File file) {
            Log.i(TAG, file.getAbsolutePath());
            showResult(originPhotos, file);
        }

        @Override
        public void onError(Throwable e) {
        }
    }).launch();
}
Also used : OnRenameListener(top.zibin.luban.OnRenameListener) OnCompressListener(top.zibin.luban.OnCompressListener) BigInteger(java.math.BigInteger) CompressionPredicate(top.zibin.luban.CompressionPredicate) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) File(java.io.File)

Aggregations

File (java.io.File)4 OnCompressListener (top.zibin.luban.OnCompressListener)4 ArrayMap (android.support.v4.util.ArrayMap)1 PostMessageImageItem (com.connxun.ltcx.entity.PostMessageImageItem)1 StatusApi (com.funstill.kelefun.data.api.StatusApi)1 Status (com.funstill.kelefun.data.model.Status)1 SignInterceptor (com.funstill.kelefun.http.SignInterceptor)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 BigInteger (java.math.BigInteger)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1 CompressionPredicate (top.zibin.luban.CompressionPredicate)1 OnRenameListener (top.zibin.luban.OnRenameListener)1