(原创)Bitmap工具类集合

这边做一个博客,专门用来放一些Bitmap的基本操作方法

再分享一个bitmap优化的博客:

Android性能优化系列:Bitmap

缩放(顺带压缩)

    /**
     * 缩放图片的宽高
     * 也可以达到压缩的目的,
     * 和修改inSampleSize的方式有异曲同工之妙
     * @param bitmap
     * @param sx
     * @param sy
     * @return
     */
    public static Bitmap CompressBitmapForScale(Bitmap bitmap, float sx, float sy) {
        Matrix matrix = new Matrix();
        matrix.setScale(sx, sy);
        Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return newBmp;
    }
    /**
     * 压缩成指定宽高
     * 其实和CompressBitmapForScale原理是一样的
     * 也是改变宽高,只不过这里我们只需要传入原来的bitmap
     * 内部自动帮我们算好了缩放比例而已
     * @param bitmap
     * @param width
     * @param height
     * @return
     */
    public static Bitmap CompressBitmapForWH(Bitmap bitmap, int width, int height) {
       return Bitmap.createScaledBitmap(bitmap, width, height, true);
    }

压缩

    /**
     * 通过修改inSampleSize采样率达到压缩目的
     * 2代表宽高都除于2,这样图片就变成原来的四分之一
     * @param sourceBitmap
     * @return
     */
    public static Bitmap CompressBitmapForSampleSize(String sourceBitmap) {
        BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
        bitmapFactoryOptions.inJustDecodeBounds = true;
        bitmapFactoryOptions.inSampleSize = 2;
        // 这里一定要将其设置回false,因为之前我们将其设置成了true
        // 设置inJustDecodeBounds为true后,decodeFile并不分配空间,即,BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度
        bitmapFactoryOptions.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(sourceBitmap, bitmapFactoryOptions);
        return bmp;
    }
    /**
     * 忽略透明度达到压缩目的
     * 经过测试,压缩后大小为原来的二分之一
     * @param sourceBitmap
     * @return
     */
    public static Bitmap CompressBitmapForRGB(String sourceBitmap) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //获取资源图片
        return BitmapFactory.decodeFile(sourceBitmap, opt);
    }
    /**
     * 压缩的是只是存储大小,而不是图片在内存的大小
     * 具体看:https://www.cnblogs.com/shoneworn/p/6932638.html
     * quality:压缩为原来的百分之多少,这里就是10%
     * @param bitmap
     * @return
     */
    public static Bitmap CompressBitmapForQuality(Bitmap bitmap){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
        bitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos);
        //把压缩后的数据baos存放到ByteArrayInputStream中
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        //把ByteArrayInputStream数据生成图片
        Bitmap newBitmap = BitmapFactory.decodeStream(isBm, null, null);
        return newBitmap;
    }

url转bitmap

    public static Bitmap getBitmap(String url) {
        URL imageURL = null;
        Bitmap bitmap = null;
        try {
            imageURL = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) imageURL
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

bitmap切片重组

这个主要是加载一个大的bitmap时使用,

将大的bitmap切为多个小的bitmap,并放入列表中

最后也可以选择返回列表,或者遍历列表的bitmap,重组为大的bitmap

public void setBitmapToImg(Bitmap resource, ImageView mImageView1) {
        Rect mRect = new Rect();
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            resource.compress(Bitmap.CompressFormat.PNG, 100, baos);

            InputStream isBm = new ByteArrayInputStream(baos.toByteArray());

            //BitmapRegionDecoder newInstance(InputStream is, boolean isShareable)
            //用于创建BitmapRegionDecoder,isBm表示输入流,只有jpeg和png图片才支持这种方式,
            // isShareable如果为true,那BitmapRegionDecoder会对输入流保持一个表面的引用,
            // 如果为false,那么它将会创建一个输入流的复制,并且一直使用它。即使为true,程序也有可能会创建一个输入流的深度复制。
            // 如果图片是逐步解码的,那么为true会降低图片的解码速度。如果路径下的图片不是支持的格式,那就会抛出异常
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(isBm, true);

            final int imgWidth = decoder.getWidth();
            final int imgHeight = decoder.getHeight();

            BitmapFactory.Options opts = new BitmapFactory.Options();

            //计算图片要被切分成几个整块,
            // 如果sum=0 说明图片的长度不足3000px,不进行切分 直接添加
            // 如果sum>0 先添加整图,再添加多余的部分,否则多余的部分不足3000时底部会有空白
            int sum = imgHeight / 3000;

            int redundant = imgHeight % 3000;

            List<Bitmap> bitmapList = new ArrayList<>();

            //说明图片的长度 < 3000
            if (sum == 0) {
                //直接加载
                bitmapList.add(resource);
            } else {
                //说明需要切分图片
                for (int i = 0; i < sum; i++) {
                    //需要注意:mRect.set(left, top, right, bottom)的第四个参数,
                    //也就是图片的高不能大于这里的4096
                    mRect.set(0, i * 3000, imgWidth, (i + 1) * 3000);
                    Bitmap bm = decoder.decodeRegion(mRect, opts);
                    bitmapList.add(bm);
                }

                //将多余的不足3000的部分作为尾部拼接
                if (redundant > 0) {
                    mRect.set(0, sum * 3000, imgWidth, imgHeight);
                    Bitmap bm = decoder.decodeRegion(mRect, opts);
                    bitmapList.add(bm);
                }

            }

            Bitmap bigbitmap = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888);
            Canvas bigcanvas = new Canvas(bigbitmap);

            Paint paint = new Paint();
            int iHeight = 0;

            //将之前的bitmap取出来拼接成一个bitmap
            for (int i = 0; i < bitmapList.size(); i++) {
                Bitmap bmp = bitmapList.get(i);
                bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
                iHeight += bmp.getHeight();

                bmp.recycle();
                bmp = null;
            }

            mImageView1.setImageBitmap(bigbitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Bitmap和String互转

(原创)分享自己写的几个工具类(七)Bitmap和String互转

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 C++ 中接收并显示 Android 上的 Bitmap,您可以使用 Android 提供的 Bitmap 工具类来完成此操作。以下是一些基本步骤: 1.在 Java 中将 Bitmap 转换为 byte 数组,然后将其传递给 C++ 函数。 ```java Bitmap bitmap = ...; // 获取 Bitmap int bytes = bitmap.getByteCount(); ByteBuffer buffer = ByteBuffer.allocate(bytes); bitmap.copyPixelsToBuffer(buffer); byte[] byteArray = buffer.array(); nativeFunction(byteArray, bitmap.getWidth(), bitmap.getHeight()); ``` 2.在 C++ 中,您需要使用 JNI 将传递的字节数组转换回 Bitmap。您可以使用 Android 提供的 Bitmap 工具类来完成此操作。以下是一个示例: ```c++ extern "C" JNIEXPORT void JNICALL Java_com_example_myapp_NativeBridge_nativeFunction(JNIEnv *env, jobject thiz, jbyteArray byteArray, jint width, jint height) { jbyte *data = env->GetByteArrayElements(byteArray, nullptr); if (data == nullptr) { return; } jclass bitmapConfigClass = env->FindClass("android/graphics/Bitmap$Config"); jmethodID bitmapConfigMethod = env->GetStaticMethodID(bitmapConfigClass, "valueOf", "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;"); jstring bitmapConfigName = env->NewStringUTF("ARGB_8888"); jobject bitmapConfig = env->CallStaticObjectMethod(bitmapConfigClass, bitmapConfigMethod, bitmapConfigName); jclass bitmapClass = env->FindClass("android/graphics/Bitmap"); jmethodID createBitmapMethod = env->GetStaticMethodID(bitmapClass, "createBitmap", "([BIILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;"); jbyteArray byteArray = env->NewByteArray(bytes); env->SetByteArrayRegion(byteArray, 0, bytes, data); jobject bitmap = env->CallStaticObjectMethod(bitmapClass, createBitmapMethod, byteArray, width, height, bitmapConfig); env->DeleteLocalRef(bitmapConfigName); env->DeleteLocalRef(byteArray); env->ReleaseByteArrayElements(byteArray, data, JNI_ABORT); // 将 Bitmap 显示到屏幕上 ... } ``` 在这里,我们使用了 JNI 中的一些重要函数(例如 `env->FindClass`、`env->GetStaticMethodID`、`env->CallStaticObjectMethod` 等)来找到并调用 Android 提供的 Bitmap 工具类中的方法。我们创建了一个新的 Bitmap,并将其显示到屏幕上。 请注意,这只是一个基本示例,您需要根据自己的需求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值