本文整理汇总了Java中android.graphics.Bitmap.copy方法的典型用法代码示例。如果您正苦于以下问题:Java Bitmap.copy方法的具体用法?Java Bitmap.copy怎么用?Java Bitmap.copy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.graphics.Bitmap
的用法示例。
在下文中一共展示了Bitmap.copy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: brightBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static Bitmap brightBitmap(Bitmap bitmap, int brightness) {
float[] colorTransform = {
1, 0, 0, 0, brightness,
0, 1, 0, 0, brightness,
0, 0, 1, 0, brightness,
0, 0, 0, 1, 0};
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0f);
colorMatrix.set(colorTransform);
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
Bitmap resultBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, paint);
return resultBitmap;
}
示例2: highlightSelectedFaceThumbnail
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static Bitmap highlightSelectedFaceThumbnail(Bitmap originalBitmap) {
Bitmap bitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.parseColor("#3399FF"));
int stokeWidth = Math.max(originalBitmap.getWidth(), originalBitmap.getHeight()) / 10;
if (stokeWidth == 0) {
stokeWidth = 1;
}
bitmap.getWidth();
paint.setStrokeWidth(stokeWidth);
canvas.drawRect(
0,
0,
bitmap.getWidth(),
bitmap.getHeight(),
paint);
return bitmap;
}
示例3: addImageWatermark
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 添加图片水印
*
* @param src 源图片
* @param watermark 图片水印
* @param x 起始坐标x
* @param y 起始坐标y
* @param alpha 透明度
* @param recycle 是否回收
* @return 带有图片水印的图片
*/
public static Bitmap addImageWatermark(Bitmap src, Bitmap watermark, int x, int y, int alpha, boolean recycle) {
if (isEmptyBitmap(src)) {
return null;
}
Bitmap ret = src.copy(src.getConfig(), true);
if (!isEmptyBitmap(watermark)) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas(ret);
paint.setAlpha(alpha);
canvas.drawBitmap(watermark, x, y, paint);
}
if (recycle && !src.isRecycled()) {
src.recycle();
}
return ret;
}
示例4: handleMessage
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Override
public void handleMessage(Message message) {
switch (message.what) {
case MESSAGE_RESTART_PREVIEW:
restartPreviewAndDecode();
break;
case MESSAGE_DECODE_SUCCEEDED:
state = State.SUCCESS;
Bundle bundle = message.getData();
Bitmap barcode = null;
float scaleFactor = 1.0f;
if (bundle != null) {
byte[] compressedBitmap = bundle.getByteArray(DecodeThread.BARCODE_BITMAP);
if (compressedBitmap != null) {
barcode = BitmapFactory.decodeByteArray(compressedBitmap, 0, compressedBitmap.length, null);
// Mutable copy:
barcode = barcode.copy(Bitmap.Config.ARGB_8888, true);
}
scaleFactor = bundle.getFloat(DecodeThread.BARCODE_SCALED_FACTOR);
}
barcodeReaderView.handleDecode((Result) message.obj, barcode, scaleFactor);
break;
case MESSAGE_DECODE_FAILED:
// We're decoding as fast as possible, so when one decode fails, start another.
state = State.PREVIEW;
cameraManager.requestPreviewFrame(decodeThread.getHandler(), MESSAGE_DECODE);
break;
}
}
示例5: addTextWatermark
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 添加文字水印
*
* @param src 源图片
* @param content 水印文本
* @param textSize 水印字体大小
* @param color 水印字体颜色
* @param x 起始坐标x
* @param y 起始坐标y
* @param recycle 是否回收
* @return 带有文字水印的图片
*/
public static Bitmap addTextWatermark(Bitmap src, String content, float textSize, int color, float x,
float y, boolean recycle) {
if (isEmptyBitmap(src) || content == null) return null;
Bitmap ret = src.copy(src.getConfig(), true);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas(ret);
paint.setColor(color);
paint.setTextSize(textSize);
Rect bounds = new Rect();
paint.getTextBounds(content, 0, content.length(), bounds);
canvas.drawText(content, x, y + textSize, paint);
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
示例6: addImageWatermark
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 添加图片水印
*
* @param src 源图片
* @param watermark 图片水印
* @param x 起始坐标x
* @param y 起始坐标y
* @param alpha 透明度
* @param recycle 是否回收
* @return 带有图片水印的图片
*/
public static Bitmap addImageWatermark(final Bitmap src, final Bitmap watermark, final int x, final int y, final int alpha, final boolean recycle) {
if (isEmptyBitmap(src)) return null;
Bitmap ret = src.copy(src.getConfig(), true);
if (!isEmptyBitmap(watermark)) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas(ret);
paint.setAlpha(alpha);
canvas.drawBitmap(watermark, x, y, paint);
}
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
示例7: cropBitmapObjectWithScale
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Crop image bitmap from given bitmap using the given points in the original bitmap and the given rotation.<br>
* if the rotation is not 0,90,180 or 270 degrees then we must first crop a larger area of the image that
* contains the requires rectangle, rotate and then crop again a sub rectangle.
*
* @param scale how much to scale the cropped image part, use 0.5 to lower the image by half (OOM handling)
*/
private static Bitmap cropBitmapObjectWithScale(Bitmap bitmap, float[] points, int degreesRotated,
boolean fixAspectRatio, int aspectRatioX, int aspectRatioY, float scale) {
// get the rectangle in original image that contains the required cropped area (larger for non rectangular crop)
Rect rect = getRectFromPoints(points, bitmap.getWidth(), bitmap.getHeight(), fixAspectRatio, aspectRatioX, aspectRatioY);
// crop and rotate the cropped image in one operation
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
matrix.postRotate(degreesRotated, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
Bitmap result = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height(), matrix, true);
if (result == bitmap) {
// corner case when all bitmap is selected, no worth optimizing for it
result = bitmap.copy(bitmap.getConfig(), false);
}
// rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping
if (degreesRotated % 90 != 0) {
// extra crop because non rectangular crop cannot be done directly on the image without rotating first
result = cropForRotatedImage(result, points, rect, degreesRotated, fixAspectRatio, aspectRatioX, aspectRatioY);
}
return result;
}
示例8: adjust
import android.graphics.Bitmap; //导入方法依赖的package包/类
private Bitmap adjust(Bitmap src) {
int to = Color.RED;
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);
return bitmap;
}
示例9: getImageBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
public Bitmap getImageBitmap(String username, int size)
{
Bitmap bitmap = cache.get(getKey(username, size));
if (bitmap != null && !bitmap.isRecycled())
{
Bitmap.Config config = bitmap.getConfig();
return bitmap.copy(config, false);
}
return null;
}
示例10: addImageWatermark
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 添加图片水印
*
* @param src 源图片
* @param watermark 图片水印
* @param x 起始坐标x
* @param y 起始坐标y
* @param alpha 透明度
* @param recycle 是否回收
* @return 带有图片水印的图片
*/
public static Bitmap addImageWatermark(Bitmap src, Bitmap watermark, int x, int y, int alpha, boolean recycle) {
if (isEmptyBitmap(src)) return null;
Bitmap ret = src.copy(src.getConfig(), true);
if (!isEmptyBitmap(watermark)) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas(ret);
paint.setAlpha(alpha);
canvas.drawBitmap(watermark, x, y, paint);
}
if (recycle && !src.isRecycled()) src.recycle();
return ret;
}
示例11: drawTextToBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {
Log.i(TAG, "drawTextToBitmap = " + gText);
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap =
BitmapFactory.decodeResource(resources, gResId);
Bitmap.Config bitmapConfig =
bitmap.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.WHITE);
// text size in pixels
paint.setTextSize((int) (12 * scale));
// text shadow
// paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(gText, 0, gText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/2;
int y = (bitmap.getHeight())/2 + (int)scale*2;
canvas.drawText(gText, x, y, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bitmap;
}
示例12: doBlur
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* blur a given bitmap
*
* @param sentBitmap bitmap to blur
* @param radius blur radius
* @param canReuseInBitmap true if bitmap must be reused without blur
* @param context used by RenderScript, can be null if RenderScript disabled
* @return blurred bitmap
*/
public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap, Context context) {
Bitmap bitmap;
if (canReuseInBitmap) {
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
}
if (bitmap.getConfig() == Bitmap.Config.RGB_565) {
// RenderScript hates RGB_565 so we convert it to ARGB_8888
// (see http://stackoverflow.com/questions/21563299/
// defect-of-image-with-scriptintrinsicblur-from-support-library)
bitmap = convertRGB565toARGB888(bitmap);
}
try {
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;
} catch (RSRuntimeException e) {
Log.e(TAG, "RenderScript known error : https://code.google.com/p/android/issues/detail?id=71347 "
+ "continue with the FastBlur approach.");
}
return null;
}
示例13: blur
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Override
public Bitmap blur(Bitmap original, float radius) {
int width = original.getWidth();
int height = original.getHeight();
Bitmap blurred = original.copy(Bitmap.Config.ARGB_8888, true);
ScriptC_blur blurScript = new ScriptC_blur(_rs, context.getResources(), R.raw.blur);
Allocation inAllocation = Allocation.createFromBitmap(_rs, blurred, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
blurScript.set_gIn(inAllocation);
blurScript.set_width(width);
blurScript.set_height(height);
blurScript.set_radius((int) radius);
int[] row_indices = new int[height];
for (int i = 0; i < height; i++) {
row_indices[i] = i;
}
Allocation rows = Allocation.createSized(_rs, Element.U32(_rs), height, Allocation.USAGE_SCRIPT);
rows.copyFrom(row_indices);
row_indices = new int[width];
for (int i = 0; i < width; i++) {
row_indices[i] = i;
}
Allocation columns = Allocation.createSized(_rs, Element.U32(_rs), width, Allocation.USAGE_SCRIPT);
columns.copyFrom(row_indices);
blurScript.forEach_blur_h(rows);
blurScript.forEach_blur_v(columns);
inAllocation.copyTo(blurred);
return blurred;
}
示例14: drawZoomRegion
import android.graphics.Bitmap; //导入方法依赖的package包/类
public void drawZoomRegion(Bitmap parentContent, Rect sourceRect, float scaleFactor) {
this.mParentContent = parentContent;
mZoomedRegion = new Rect((int) (sourceRect.left / scaleFactor), (int) (sourceRect.top / scaleFactor),
(int) (sourceRect.right / scaleFactor), (int) (sourceRect.bottom / scaleFactor));
mSourceRect = new Rect(0, 0, mParentContent.getWidth(), mParentContent.getHeight());
mDestRect = new Rect(0, 0, getWidth(), getHeight());
Bitmap init = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
mZoomOverlay = init.copy(Bitmap.Config.ARGB_8888, true);
init.recycle();
mCanvasOverlay = new Canvas(mZoomOverlay);
invalidate();
}
示例15: decodeBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Decodes bitmap (maybe immutable) that keeps aspect-ratio and spans most within the bounds.
*/
public Bitmap decodeBitmap(byte[] data, int width, int height){
Log.i(TAG, "width = " + width + " , " + "height = " + height);
Bitmap bitmap = null;
try {
// TODO: Take max pixels allowed into account for calculation to avoid possible OOM.
Rect bounds = getBitmapBounds(data);
int sampleSize = Math.max(bounds.width() / width, bounds.height() / height);
sampleSize = Math.min(sampleSize,
Math.max(bounds.width() / height, bounds.height() / width));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = Math.max(sampleSize, 1);
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Log.i(TAG, "sampleSize = " + sampleSize + " , " + "options.inSampleSize = " + options.inSampleSize);
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);//!!!!溢出
} catch (Exception e) {
Log.e(TAG, e.getMessage());
} finally {
data = null;
}
// Ensure bitmap in 8888 format, good for editing as well as GL compatible.
if ((bitmap != null) && (bitmap.getConfig() != Bitmap.Config.ARGB_8888)) {
Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
bitmap.recycle();
bitmap = copy;
}
if (bitmap != null) {
// Scale down the sampled bitmap if it's still larger than the desired dimension.
float scale = Math.min((float) width / bitmap.getWidth(),
(float) height / bitmap.getHeight());
scale = Math.max(scale, Math.min((float) height / bitmap.getWidth(),
(float) width / bitmap.getHeight()));
if (scale < 1) {
Matrix m = new Matrix();
m.setScale(scale, scale);
Bitmap transformed = createBitmap(
bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m);
bitmap.recycle();
return transformed;
}
}
return bitmap;
}