学习了一下压缩和水印,以后要用到的时候可以直接来这里拷贝 
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bitmap_compress.MainActivity" > <ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="300sp"
android:contentDescription="@null"
android:src="@drawable/a" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> </LinearLayout>

MainActivity.Java

package com.example.bitmap_compress;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity {
private ImageView imageView;
private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//图片压缩
imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = BitmapCompressTools.decodeSampledBitmapFromResource(
getResources(), R.drawable.a, 100, 100);
Log.i("Original bytes",
"----->>>"
+ BitmapFactory.decodeResource(getResources(),
R.drawable.a).getByteCount());
Log.i("Compressed bytes", "----->>>" + bitmap.getByteCount());
imageView.setImageBitmap(bitmap); //水印,其实就是往图片上写字
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = BitmapTools.createBitmap(getResources(),
R.drawable.a, "a.png");
imageView.setImageBitmap(bitmap);
}
});
}
}

然后给出每个的工具类 
BitmapTools.java

package com.example.bitmap_compress;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream; import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Environment; public class BitmapTools { public static Bitmap createBitmap(Resources resources, int resid, String name) {
Bitmap bitmap = BitmapFactory.decodeResource(resources, resid);
// 复制一份新的Bitmap,因为不能直接在原有的bitmap上进行水印操作
// Bitmap.config存储的格式
Bitmap newBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
// 使用自定义画布
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint();
paint.setTextSize(200);
canvas.drawText("hello", 100, 100, paint);
// 判断SDcard是否在可用状态
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 直接将图片保存在根目录下
File root = Environment.getExternalStorageDirectory();
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(new File(root, name));
// 对图片进行压缩并以png格式,保存在sdcard中
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
} catch (Exception e) {
}
}
return newBitmap;
}
}

BitmapCompressTools.java

package com.example.bitmap_compress;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; public class BitmapCompressTools { public static Bitmap decodeSampledBitmapFromResource(Resources resources,
int resId, int width, int height) {
// 给定的BitmapFactory设置解码的参数
BitmapFactory.Options options = new BitmapFactory.Options();
// 从解码器中获得原始图片的宽高,而避免申请内存空间
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, resId, options);
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, resId, options);
} /**
* 指定输出图片的缩放比例
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// 获得原始图片的宽高
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
int inSimpleSize = 1;
if (imageHeight > reqHeight || imageWidth > reqWidth) { // 计算压缩的比例:分为宽高比例
final int heightRatio = Math.round((float) imageHeight
/ (float) reqHeight);
final int widthRatio = Math.round((float) imageWidth
/ (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSimpleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSimpleSize;
}
}

android图片的压缩和水印的更多相关文章

  1. Android图片处理——压缩、剪裁、圆角、保存

    点击查看原文 项目中用到的关于图片的处理 public class UtilPicture { public static final String IMAGE_UNSPECIFIED = " ...

  2. Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案

    1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...

  3. Android图片压缩方法总结

    本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩).   第一:质量压缩方法:   ? 1 2 3 ...

  4. android图片压缩方法

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  5. Android之获取本地图片并压缩方法

    这两天在做项目时,做到上传图片功能一块时,碰到两个问题,一个是如何获取所选图片的路径,一个是如何压缩图片,在查了一些资料和看了别人写的后总算折腾出来了,在此记录一下. 首先既然要选择图片,我们就先要获 ...

  6. android图片压缩的3种方法实例

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  7. Android实现图片的压缩、旋转工具类

    import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matri ...

  8. 性能优化——Android图片压缩与优化的几种方式

    图片优化压缩方式大概可以分为以下几类:更换图片格式,质量压缩,采样率压缩,缩放压缩,调用jpeg压缩等1.设置图片格式Android目前常用的图片格式有png,jpeg和webp,png:无损压缩图片 ...

  9. android图片压缩总结

    一.bitmap 图片格式介绍 android中图片是以bitmap形式存在的,那么bitmap所占内存,直接影响到了应用所占内存大小,首先要知道bitmap所占内存大小计算方式: bitmap内存大 ...

随机推荐

  1. 安装Ubuntu双系统系列——更换源

    Ubuntu 有一个非常有用的命令 apt-get,它可以帮助你下载软件,还可以安装,下载并安装的命令是 apt-get install. 那Ubuntu默认是从哪里下载软件呢,这可以查看文件/etc ...

  2. Nandflash 驱动移植

    前段时间,研究了一下4G的Nandflash驱动.手头上只有飞凌6410BSP自带的Nandflash驱动,该驱动不支持K9GAG08U0D(2G)和K9LBG08U0D(4G)的Nandflash. ...

  3. 【HDOJ】3007 Buried memory

    1. 题目描述有n个点,求能覆盖这n个点的半径最小的圆的圆心及半径. 2. 基本思路算法模板http://soft.cs.tsinghua.edu.cn/blog/?q=node/1066定义Di表示 ...

  4. 实用的eclipse adt 快捷键

    Ctrl + Shift + T: 打开类型:显示"打开类型"对话框来在编辑器中打开类型."打开类型"选择对话框显示工作空间中存在的所有类型如类.接口等.    ...

  5. Compass被墙后如何安装安装

    今天安装 Compass 多时候发现竟然安装不了,且什么提示也没有,让人纳闷.安装代码如下: $ gem install compass 运行该段代码后发现没反应,也没有提示,后来网上查了才知道,竟然 ...

  6. 1126. Magnetic Storms(单调队列)

    1126 最简单的单调队列应用吧 单调队列是指在一个队列中各个元素单调 递增(或者递减),并且各个元素的下标单调 递增. 单调队列的大体操作 进队时,将进队的元素为e,从队尾往前扫描,直到找到一个不大 ...

  7. 三个流行MySQL分支的对比

    MySQL是历史上最受欢迎的免费开源程序之一.它是成千上万个网站的数据库骨干,并且可以将它(和Linux)作为过去10年里Internet呈指数级增长的一个有力证明. 那么,如果MySQL真的这么重要 ...

  8. 【Java基础之容器】Iterator

    Iterator: ->所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象 ->Iterator对象称作迭代器,用以方便的实 ...

  9. 【转】Git详解之一:Git起步

    原文网址:http://blog.jobbole.com/25775/ 原文:<Pro Git> 起步 本章介绍开始使用 Git 前的相关知识.我们会先了解一些版本控制工具的历史背景,然后 ...

  10. SpringSide 3 中的多数据源配置的问题

    在SpringSide 3 中,白衣提供的预先配置好的环境非常有利于用户进行快速开发,但是同时也会为扩展带来一些困难.最直接的例子就是关于在项目中使用多个数据源的问题,似乎很难搞.在上一篇中,我探讨了 ...