学习了一下压缩和水印,以后要用到的时候可以直接来这里拷贝 
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. libevent入门教程

    首先给出官方文档吧: http://libevent.org ,首页有个Programming with Libevent,里面是一节一节的介绍libevent,但是感觉信息量太大了,而且还是英文的- ...

  2. Android开发之ADT导入Support Library

    在工程中增加(例如 support-v4 Library) 在ADT中需要按照以下步骤:  1.右击当前工程,查找Properties 2.选择Java Build Path 3.选择Librarie ...

  3. 1067. Disk Tree(字符串)

    1067 破题啊  写完发现理解错题意了 子目录下会有跟之前重名的 把输入的字符串存下来 排下序 然后依次找跟上面有没有重的 #include <iostream> #include< ...

  4. 2013 ACM/ICPC Asia Regional Changsha Online - E

    第一个被板刷的题 取余 依次算在周几 #include <iostream> #include<cstdio> #include<cstring> #include ...

  5. poj3321Apple Tree(树状数组)

    http://poj.org/problem?id=3321 刚一看题以为要建一颗树 看了下讨论说dfs 这里dfs遍历时设的标号很好 一个low一个high 包含了以这一节点为根节点的子树结点的所有 ...

  6. bzoj2906

    显然分块,由于颜色也有区间,我们的ans[l,r,k]表示块l和块r颜色1~k的权值和所以我们块的大小要设为n^(2/3),其它没什么说的,比较水 ..,..,..] of int64; g:..,. ...

  7. 2434: [Noi2011]阿狸的打字机

    ac自动机,bit,dfs序. 本文所有的stl都是因为自己懒得实现.   首先x在y里面出现,就意味y节点可以顺着fail回去. 反向建出一个fail数,然后搞出dfs序列.找出x对应的区间有多少个 ...

  8. poj2001 Shortest Prefixes (trie)

    读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数. 然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了. 不知为何下面的程序一直有bug……不知是读入的问题? type nod ...

  9. angularjs的事件 $broadcast and $emit and $on

    angularjs的事件 $broadcast and $emit and $on <!DOCTYPE html> <html> <head> <meta c ...

  10. 中国Azure媒体服务RESTAPI的Endpoint

    Amber Zhao  Thu, Feb 26 2015 4:09 AM 由于海外Azure和中国Azure有不同的domain,很多用户在使用媒体服务RESTAPI时,需要指定中国Azure媒体服务 ...