欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

android 图片压缩示例源码

介绍 评论 失效链接反馈

from clipboardpackage id.zelory.compressor.sample;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Color;import android.os.Bundle;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import java.io.File;import java.io.IOException;import java.text.DecimalFormat;import java.util.Random;import id.zelory.compressor.Compressor;import io.reactivex.android.schedulers.AndroidSchedulers;import io.reactivex.functions.Consumer;import io.reactivex.schedulers.Schedulers;public class MainActivity extends AppCompatActivity { private static final int PICK_IMAGE_REQUEST = 1; private ImageView actualImageView; private ImageView compressedImageView; private TextView actualSizeTextView; private TextView compressedSizeTextView; private File actualImage; private File compressedImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); actualImageView = (ImageView) findViewById(R.id.actual_image); compressedImageView = (ImageView) findViewById(R.id.compressed_image); actualSizeTextView = (TextView) findViewById(R.id.actual_size); compressedSizeTextView = (TextView) findViewById(R.id.compressed_size); actualImageView.setBackgroundColor(getRandomColor()); clearImage(); } public void chooseImage(View view) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, PICK_IMAGE_REQUEST); } public void compressImage(View view) { if (actualImage == null) { showError("Please choose an image!"); } else { // Compress image in main thread //compressedImage = new Compressor(this).compressToFile(actualImage); //setCompressedImage(); // Compress image to bitmap in main thread //compressedImageView.setImageBitmap(new Compressor(this).compressToBitmap(actualImage)); // Compress image using RxJava in background thread new Compressor(this) .compressToFileAsFlowable(actualImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<File>() { @Override public void accept(File file) { compressedImage = file; setCompressedImage(); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) { throwable.printStackTrace(); showError(throwable.getMessage()); } }); } } public void customCompressImage(View view) { if (actualImage == null) { showError("Please choose an image!"); } else { // Compress image in main thread using custom Compressor try { compressedImage = new Compressor(this) .setMaxWidth(640) .setMaxHeight(480) .setQuality(75) .setCompressFormat(Bitmap.CompressFormat.WEBP) .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath()) .compressToFile(actualImage); setCompressedImage(); } catch (IOException e) { e.printStackTrace(); showError(e.getMessage()); } // Compress image using RxJava in background thread with custom Compressor /*new Compressor(this) .setMaxWidth(640) .setMaxHeight(480) .setQuality(75) .setCompressFormat(Bitmap.CompressFormat.WEBP) .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath()) .compressToFileAsFlowable(actualImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<File>() { @Override public void accept(File file) { compressedImage = file; setCompressedImage(); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) { throwable.printStackTrace(); showError(throwable.getMessage()); } });*/ } } private void setCompressedImage() { compressedImageView.setImageBitmap(BitmapFactory.decodeFile(compressedImage.getAbsolutePath())); compressedSizeTextView.setText(String.format("Size : %s", getReadableFileSize(compressedImage.length()))); Toast.makeText(this, "Compressed image save in " compressedImage.getPath(), Toast.LENGTH_LONG).show(); Log.d("Compressor", "Compressed image save in " compressedImage.getPath()); } private void clearImage() { actualImageView.setBackgroundColor(getRandomColor()); compressedImageView.setImageDrawable(null); compressedImageView.setBackgroundColor(getRandomColor()); compressedSizeTextView.setText("Size : -"); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) { if (data == null) { showError("Failed to open picture!"); return; } try { actualImage = FileUtil.from(this, data.getData()); actualImageView.setImageBitmap(BitmapFactory.decodeFile(actualImage.getAbsolutePath())); actualSizeTextView.setText(String.format("Size : %s", getReadableFileSize(actualImage.length()))); clearImage(); } catch (IOException e) { showError("Failed to read picture data!"); e.printStackTrace(); } } } public void showError(String errorMessage) { Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show(); } private int getRandomColor() { Random rand = new Random(); return Color.argb(100, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); } public String getReadableFileSize(long size) { if (size <= 0) { return "0"; } final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"}; int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) " " units[digitGroups]; }}

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复