Bitmap二次采样

页面布局***

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android
xmlns:app=“http://schemas.android.com/apk/res-auto
xmlns:tools=“http://schemas.android.com/tools
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">

<ImageView
    android:id="@+id/icon1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@id/icon2"/>

<ImageView
    android:id="@+id/icon2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/icon1"
    app:layout_constraintBottom_toTopOf="@id/icon3"/>

<ImageView
    android:id="@+id/icon3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/icon2"
    app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

activity代码*********

package com.example.day2;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import static android.os.Environment.DIRECTORY_DOWNLOADS;

public class MainActivity extends AppCompatActivity {

//1536
private ImageView mIcon1;
private ImageView mIcon2;
private ImageView mIcon3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();

    Log.i("TEST", "imageview宽高: " + mIcon1.getWidth() + ", " + mIcon1.getHeight());

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            initData();
        }
    }, 1000);
}

//二次采样为了做什么:避免oom(内存溢出),通过减少加载图片的分辨率
private void initData() {
    //字节   计算机处理数据的基本单位
    //为了简化创建Bitmap对象,android提供了工厂类来创建
    //工厂设计模式:简化对象的创建;隐藏内部实现细节
    //Environment.getExternalStorageDirectory();//sd卡路径
    //getCacheDir();//应用缓存路径
    //2.3   16m   32m
    // > 100m
    File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS), "1.jpg");
    //Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    //一上来立即获取ImageView控件的宽高获取不到
    Bitmap bitmap = loadBitmap(file.getAbsolutePath(), mIcon1.getWidth(), mIcon1.getHeight());
    mIcon1.setImageBitmap(bitmap);

    file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS), "2.jpg");
    //bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    bitmap = loadBitmap(file.getAbsolutePath(), mIcon1.getWidth(), mIcon1.getHeight());
    mIcon2.setImageBitmap(bitmap);

    file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS), "3.jpg");
    //bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    bitmap = loadBitmap(file.getAbsolutePath(), mIcon1.getWidth(), mIcon1.getHeight());
    mIcon3.setImageBitmap(bitmap);
}

private Bitmap loadBitmap(String path, int width, int height) {
    //经过两次加载
    //第一次只加载图片宽高等基本信息,不会将Bitmap加载到内存中
    BitmapFactory.Options options = new BitmapFactory.Options();
    //不把Bitmpa的像素加载到内存中,只是获取图片的基本信息:宽高等
    options.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    Log.i("TEST", "图片的原始宽高信息: " + options.outWidth + ", " + options.outHeight);
    Log.i("TEST", "inJustDecodeBounds true 返回: " + bitmap);
    Log.i("TEST", "请求图片的宽高: " + width + ", " + height);
    //采样率,最终的值系统会自动近似到2的倍数。
    //如果采样率是4,则加载图片的宽高都是原图宽高的1/4。即1/16  -> 2的4次方
    //以2的n次方进行缩放
    options.inSampleSize = Math.max(options.outWidth / width, options.outHeight / height);
    Log.i("TEST", "inSampleSize: " + options.inSampleSize);
    //第二次加载图片到内存中
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile(path, options);
    Log.i("TEST", "第二次加载的图片: " + bitmap);

    //保存二次采样后的图片
    File outSave = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS), SystemClock.currentThreadTimeMillis() + ".jpg");
    try {
        //PNG是不支持压缩的。是无损图片格式
        //JPG支持压缩
        //压缩图片并保存到流(文件)
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(outSave));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return bitmap;

    //一张图片文件占用大概是2M不到。为什么加载到内存中占用了30M
    //一张3840 * 2160像素的图片文件,总共有8294400个像素点
    //每个像素点占用多少
    //Bitmap.Config.ALPHA_8;//只有透明度,占8位即1个字节
    //Bitmap.Config.ARGB_8888;//透明度,红色,绿色,蓝色分别占用8位即1个字节。总共占用4字节
    //Bitmap.Config.RGB_565;//2个字节
    //Bitmap.Config.ARGB_4444;//2个字节
}

private void initView() {
    mIcon1 = (ImageView) findViewById(R.id.icon1);
    mIcon2 = (ImageView) findViewById(R.id.icon2);
    mIcon3 = (ImageView) findViewById(R.id.icon3);
}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将一个Android Bitmap对象转化为二进制数据,可以使用Bitmap类的compress()方法。具体的步骤如下: 1. 创建一个ByteArrayOutputStream对象。 2. 调用Bitmap对象的compress()方法,将Bitmap对象压缩为指定格式的二进制数据,并将结果输出到ByteArrayOutputStream对象中。 3. 调用ByteArrayOutputStream对象的toByteArray()方法,将ByteArrayOutputStream对象中的二进制数据转化为byte数组。 以下是一个示例代码: ```java Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] imageBytes = baos.toByteArray(); ``` 在上面的代码中,我们首先使用BitmapFactory类的decodeResource()方法,从资源文件中获取一个Bitmap对象。然后创建一个ByteArrayOutputStream对象,调用Bitmap对象的compress()方法,将Bitmap对象压缩为PNG格式的二进制数据,并将结果输出到ByteArrayOutputStream对象中。最后使用ByteArrayOutputStream对象的toByteArray()方法,将ByteArrayOutputStream对象中的二进制数据转化为byte数组。 注意,这里我们将Bitmap对象压缩为PNG格式的二进制数据,你可以根据需要将其压缩为其他格式,比如JPEG。另外,compress()方法中的第二个参数表示压缩质量,取值范围为0-100,数值越高表示压缩质量越好,但文件大小也越大。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值