Android 中 Bitmap.copy() 方法的使用
一、Bitmap.copy()
Bitmap copy (Bitmap.Config config, boolean isMutable)
Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap's config to the one specified, and then copying this bitmap's pixels into the new bitmap. If the conversion is not supported, or the allocator fails, then this returns NULL. The returned bitmap initially has the same density as the original.
根据位图的大小产生一个新位图,根据指定的结构设置新位图的结构,然后把位图的像素拷贝到新位图中。如果不支持该转换,或者分配内存失败,那就返回NULL。返回的位图和原图有同样的像素密度。
使用软引用以后,在OutOfMemory异常发生之前,这些缓存的图片资源的内存空间可以被释放掉的,从而避免内存达到上限,避免Crash发生。
需要注意的是,在垃圾回收器对这个 Java 对象回收前,SoftReference 类所提供的 get 方法会返回 Java 对象的强引用,一旦垃圾线程回收该 Java 对象之后,get方法将返回null。所以在获取软引用对象的代码中,一定要判断是否为 null,以免出现 NullPointerException 异常导致应用崩溃。
二、实例
示例一:
public static Bitmap doBlurJniArray(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {
Bitmap bitmap;
if (canReuseInBitmap) {
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
}
if (radius < 1) {
return (null);
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int[] pix = new int[w * h];
bitmap.getPixels(pix, 0, w, 0, 0, w, h);
// Jni array calculate
ImageBlur.blurIntArray(pix, w, h, radius);
bitmap.setPixels(pix, 0, w, 0, 0, w, h);
return (bitmap);
}
示例二:
Bitmap base = source.copy(Config.ARGB_8888, true);
Bitmap blend = layer.copy(Config.ARGB_8888, false);
IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight());
base.copyPixelsToBuffer(buffBase);
buffBase.rewind();
IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight());
blend.copyPixelsToBuffer(buffBlend);
buffBlend.rewind();
IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight());
buffOut.rewind();
示例三:
public static Bitmap ProductEmbed(String txt, Bitmap input, boolean colorful, int color, int x, int y, Bitmap originBitmap){
int originalSize = input.getWidth();
Bitmap qrBitmap = Product(txt, input, colorful, color);
double newScale = 1.0 * originalSize * scaleQR / (qrBitmap.getWidth() - 2 * 4 * scaleQR);
int targetSize = qrBitmap.getWidth() * originalSize / (qrBitmap.getWidth() - 2 * 4 * scaleQR);
qrBitmap = resizeQuiteZone(qrBitmap, newScale); //it does not match QR spec to cut the qr quiet zone
qrBitmap = Bitmap.createScaledBitmap(qrBitmap, targetSize, targetSize, false);
originBitmap = originBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(originBitmap);
canvas.drawBitmap(qrBitmap, x - (int) (4 * newScale), y - (int) (4 * newScale), null);
return originBitmap;
}
示例四:
// Setup to get a mutable bitmap less than 40 Kbytes
String path = "someSmallImage.jpg";
Bitmap bm0 = BitmapFactory.decodeFile(path);
// Need it mutable to change height
Bitmap bm1 = bm0.copy(bm0.getConfig(), true);
// Chop it to get a size less than 40K
bm1.setHeight(bm1.getHeight() / 32);
// Now we have a BitMap with size < 40K for the test
Bitmap bm2 = bm1.copy(bm0.getConfig(), true);
// What's the parcel size?
Parcel p1 = Parcel.obtain();
bm2.writeToParcel(p1, 0);
// Expect byteCount and allocatedByteCount to be the same
Log.i("Demo", String.format("byteCount=%d allocatedByteCount=%d parcelDataSize=%d",
bm2.getByteCount(), bm2.getAllocationByteCount(), p1.dataSize()));
// Resize to make byteCount and allocatedByteCount different
bm2.setHeight(bm2.getHeight() / 4);
// What's the parcel size?
Parcel p2 = Parcel.obtain();
bm2.writeToParcel(p2, 0);
// Show that byteCount determines size of data written to parcel
Log.i("Demo", String.format("byteCount=%d allocatedByteCount=%d parcelDataSize=%d",
bm2.getByteCount(), bm2.getAllocationByteCount(), p2.dataSize()));
p1.recycle();
p2.recycle();
示例五:
public static Bitmap getColoredBitmap(@NonNull Bitmap bitmap, @ColorInt int color) {
int alpha = Color.alpha(color);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < pixels.length; i++) {
int pixel = pixels[i];
int pixelAlpha = Color.alpha(pixel);
if (pixelAlpha != 0) {
pixels[i] = Color.argb((int) (pixelAlpha * alpha / 256f), red, green, blue);
}
}
Bitmap coloredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
coloredBitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
bitmap.getHeight());
return coloredBitmap;
}
示例六:
Bitmap base = source.copy(Config.ARGB_8888, true);
Bitmap blend = layer.copy(Config.ARGB_8888, false);
IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight());
base.copyPixelsToBuffer(buffBase);
buffBase.rewind();
IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight());
blend.copyPixelsToBuffer(buffBlend);
buffBlend.rewind();
IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight());
buffOut.rewind();
示例七:
SeekBar timerBar = (SeekBar) findViewById(R.id.seekBarTimer);
if (timerBar != null) {
timerBar.setMax((int) (Settings.countdownSeconds + 1));
timerBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar timerBar, int arg1, boolean arg2) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.seek_thumb);
Bitmap bmp = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(bmp);
String text = Integer.toString(timerBar.getProgress());
Paint p = new Paint();
p.setTypeface(Typeface.DEFAULT_BOLD);
p.setTextSize(14);
p.setColor(0xFFFFFFFF);
int width = (int) p.measureText(text);
int yPos = (int) ((c.getHeight() / 2) - ((p.descent() + p.ascent()) / 2));
c.drawText(text, (bmp.getWidth()-width)/2, yPos, p);
timerBar.setThumb(new BitmapDrawable(getResources(), bmp));
}
});
timerBar.setProgress(0);
}
示例八:
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
float factor = smallest / radius;
sbmp = Bitmap.createScaledBitmap(bmp,
(int) (bmp.getWidth() / factor),
(int) (bmp.getHeight() / factor), false);
} else {
sbmp = bmp;
}
示例九:
Bitmap bitmap = BitmapFactory.decodeResource(
getResources(), com.cinchvalet.app.client.R.drawable.slider_pop_counter);
Bitmap bmp = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(bmp);
String text = "Your text";
Paint p = new Paint();
p.setTypeface(Typeface.DEFAULT_BOLD);
p.setTextSize(35);
p.setColor(getResources().getColor(com.cinchvalet.app.client.R.color.Black));
int width = (int) p.measureText(text);
int yPos = (int) ((c.getHeight() / 2)
- ((p.descent() + p.ascent()) / 2) - 10);
c.drawText(text, (bmp.getWidth() - width) / 2, yPos, p);
示例十:
public Bitmap applyCropTo(Bitmap bitmap) {
Bitmap immutableCropped = Bitmap.createBitmap(bitmap,
findRealCoordinate(bitmap.getWidth(), cropRect.left, imageRect.width()),
findRealCoordinate(bitmap.getHeight(), cropRect.top, imageRect.height()),
findRealCoordinate(bitmap.getWidth(), cropRect.width(), imageRect.width()),
findRealCoordinate(bitmap.getHeight(), cropRect.height(), imageRect.height()));
return immutableCropped.copy(immutableCropped.getConfig(), true);
}
示例十一:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3);
paint.setTextSize(30);
Bitmap tmep = bt.copy(Config.ARGB_8888, true);
Canvas tmepCanvas = new Canvas(tmep);
tmepCanvas.drawCircle(0, 0, tmep.getWidth(), paint);
canvas.drawBitmap(tmep, 0, 200, paint);
paint.setStrokeWidth(0);
canvas.drawText("复制的原图是可以修改的", 0,400, paint);
canvas.drawBitmap(bt, 0, 0, paint);
canvas.drawText("原图", 0, 150, paint);
// 如果把原图 放到Canvas里去修改 那么会爆出这个错误
//AndroidRuntime(25875): java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
// Canvas tmepCanvas2 = new Canvas(bt);
// tmepCanvas.drawCircle(0, 0, tmep.getWidth(), paint);
// canvas.drawBitmap(bt, 300, 0, paint);
// canvas.drawText("原图不能修改", 300, 150, paint);
}
示例十二:
int width = Math.round(sentBitmap.getWidth() * scale);
int height = Math.round(sentBitmap.getHeight() * scale);
sentBitmap = Bitmap.createScaledBitmap(sentBitmap, width, height, false);
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
代码示例来源:origin: stackoverflow.com
bitmap = bitmap.copy(bitmapConfig, true);
int x = (bitmap.getWidth() - bounds.width())/6;
int y = (bitmap.getHeight() + bounds.height())/5;
代码示例来源:origin: JessYanCoding/MVPArms
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
代码示例来源:origin: qiujuer/Genius-Android
Bitmap overlay = mBitmap.copy(mBitmap.getConfig(), true);
if (mCompress) {
radius = 3;
overlay = mCompressBitmap.copy(mCompressBitmap.getConfig(), true);
} else if (i == 2) {
int w = overlay.getWidth();
int h = overlay.getHeight();
int[] pix = new int[w * h];
overlay.getPixels(pix, 0, w, 0, 0, w, h);
代码示例来源:origin: wasabeef/glide-transformations
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
代码示例来源:origin: ArthurHub/Android-Image-Cropper
getRectFromPoints(
points,
bitmap.getWidth(),
bitmap.getHeight(),
fixAspectRatio,
aspectRatioX,
matrix.setRotate(degreesRotated, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
matrix.postScale(flipHorizontally ? -scale : scale, flipVertically ? -scale : scale);
Bitmap result =
result = bitmap.copy(bitmap.getConfig(), false);
代码示例来源:origin: stackoverflow.com
Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
for(int x = 0;x < bitmap.getWidth();x++)
for(int y = 0;y < bitmap.getHeight();y++)
if(match(bitmap.getPixel(x, y)))
bitmap.setPixel(x, y, to);
代码示例来源:origin: wasabeef/Blurry
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
代码示例来源:origin: chentao0707/SimplifyReader
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
int w = bitmap.getWidth();
int h = bitmap.getHeight();