2024-07-03 java Bitmap写图像rgba数据使用setPixel和setPixels的时间对比,setPixels方法一次性设置像素会比setPixel快得惊人,相差三百多毫秒。

一、java 里面使用下面的代码使用 bitmap.setPixel逐个写像素点数据,发现程序卡顿很严重

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int index = y * width + x;
                 bitmap.setPixel(x, y, rgbaArray[index]);
            }
        }

二、然后写了下面的代码,对比一下使用setPixel 和setPixels耗费的时间,发现差距惊人,相差三百多毫秒。

public static void saveRgbaArrayAsJpeg(int[] rgbaArray, int width, int height, String filePath) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        long startTime = System.currentTimeMillis();
       for (int x = 0; x < width; x++) {
                int index = y * width + x;
                bitmap.setPixel(x, y, rgbaArray[index]);
            }
        }
  long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;

        Log.d(TAG,"setPixel total cost time: " + duration + " ms");
     startTime = System.currentTimeMillis();
        bitmap.setPixels(rgbaArray, 0, width, 0, 0, width, height);
        endTime = System.currentTimeMillis();
        duration = endTime - startTime;
   Log.d(TAG,"setPixels total cost time: " + duration + " ms");
        File file = new File(filePath);
        FileOutputStream outputStream = null;
 try {
            outputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
        } catch (IOException e) {
   e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
 e.printStackTrace();
                }
            }
        }
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值