压缩bitmap算法

public static Bitmap decodeFile(File f) {
		try {
			// decode image size
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
//			BitmapFactory.decodeStream(new FileInputStream(f), null, options);
			int inSampleSize = computeSampleSize(options, -1, 128 * 128);
			// decode with inSampleSize
			options.inSampleSize = inSampleSize;
			options.inJustDecodeBounds = false;
			return BitmapFactory.decodeStream(new FileInputStream(f), null, options);
		} catch (Exception e) {
		} 
		return null;
	}
	
	public static void copyStream(InputStream is, OutputStream os) {
		final int buffer_size = 1024;
		try {
			byte[] bytes = new byte[buffer_size];
			for (;;) {
				int count = is.read(bytes, 0, buffer_size);
				if (count == -1)
					break;
				os.write(bytes, 0, count);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * 根据实际需要,计算出合适的inSampleSize,以减少内存的开销
	 * 
	 * @param options
	 * @param minSideLength 最小长度,不限制则为-1
	 * @param maxNumOfPixels 最大像素点,128 * 128
	 * @return
	 */
	public static int computeSampleSize(BitmapFactory.Options options,
			int minSideLength, int maxNumOfPixels) {
		int initialSize = computeInitialSampleSize(options, minSideLength,
				maxNumOfPixels);

		int roundedSize;
		if (initialSize <= 8) {
			roundedSize = 1;
			while (roundedSize < initialSize) {
				roundedSize <<= 1;
			}
		} else {
			roundedSize = (initialSize + 7) / 8 * 8;
		}

		return roundedSize;
	}

	private static int computeInitialSampleSize(BitmapFactory.Options options,
			int minSideLength, int maxNumOfPixels) {
		double w = options.outWidth;
		double h = options.outHeight;
//ceil   get the integer number >parameter
		int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
				.sqrt(w * h / maxNumOfPixels));
		int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
				Math.floor(w / minSideLength), Math.floor(h / minSideLength));

		if (upperBound < lowerBound) {
			// return the larger one when there is no overlapping zone.
			return lowerBound;
		}

		if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
			return 1;
		} else if (minSideLength == -1) {
			return lowerBound;
		} else {
			return upperBound;
		}
	}

雪花算法是一种生成分布式唯一ID的算法,可以保证生成的ID在分布式系统中的唯一性。而Bitmap算法则是一种数据压缩算法,如何将这两个算法结合起来呢? 实际上,在雪花算法中,每个ID都是由时间戳、机器ID和序列号组成的。因此,我们可以将机器ID和序列号使用Bitmap算法进行压缩,从而减小ID的存储空间。 具体来说,我们可以使用两个Bitmap,一个用于存储机器ID,一个用于存储序列号。假设机器ID和序列号分别需要存储10位和12位,那么我们可以定义两个长度为(2^10)/8=128和(2^12)/8=256的byte数组,分别用于存储机器ID和序列号。 在生成ID时,先根据雪花算法生成一个64位的ID,然后将其中的机器ID和序列号分别使用Bitmap算法进行压缩,最终将压缩后的结果拼接成一个新的ID,并返回。 下面是一个简单的Java实现示例: ```java public class SnowflakeIdGenerator { private long lastTimestamp = -1L; private long sequence = 0L; private long workerId; private Bitmap workerIdBitmap; private Bitmap sequenceBitmap; public SnowflakeIdGenerator(long workerId) { this.workerId = workerId; this.workerIdBitmap = new Bitmap(1 << 10); this.sequenceBitmap = new Bitmap(1 << 12); } public synchronized long nextId() { long timestamp = System.currentTimeMillis(); if (timestamp < lastTimestamp) { throw new RuntimeException("Clock moved backwards"); } if (timestamp == lastTimestamp) { sequence = (sequence + 1) & 0xFFF; if (!sequenceBitmap.get((int) sequence)) { sequenceBitmap.set((int) sequence); } else { return nextId(); } } else { sequence = 0L; sequenceBitmap = new Bitmap(1 << 12); } lastTimestamp = timestamp; if (!workerIdBitmap.get((int) workerId)) { workerIdBitmap.set((int) workerId); } long id = ((timestamp << 22) | (workerId << 12) | sequence); return id; } } ``` 在上面的示例中,我们定义了一个SnowflakeIdGenerator类,其中包含一个机器ID和两个Bitmap。在nextId方法中,先使用雪花算法生成一个原始的64位ID,然后将其中的机器ID和序列号压缩到对应的Bitmap中,最终拼接成一个新的ID,并返回。 需要注意的是,在压缩机器ID和序列号时,我们使用了两个长度分别为1 << 10和1 << 12的Bitmap。这是因为机器ID和序列号的位数分别为10位和12位。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值