gpt4 book ai didi

安卓水印

转载 作者:行者123 更新时间:2023-11-29 16:09:02 24 4
gpt4 key购买 nike

我对 android 代码中的水印有一些疑问!

以下代码展示了我对 WaterMark 的想法!但是,它不能正常工作。

例如只有.png结尾的图片才可以做水印

有没有关于水印的方案(.jpeg, .jpg, .wbmp, .bmp, .png or others)

   protected static Bitmap getDrmPicture(Context context,String path){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

Bitmap originMap = BitmapFactory.decodeFile (path,options);
Bitmap waterMark = BitmapFactory.decodeResource(context.getResources(), R.drawable.close);

InputStream input;
byte[] b;
Bitmap waterMark = null;
try {
input = context.getResources().openRawResource(R.drawable.lock);
b = new byte[input.available()];
input.read(b);
waterMark = DecodeUtils.requestDecode(jc, b, null);
}catch(IOException e){
}

int w = originMap.getWidth();
int h = originMap.getHeight();

int ww = waterMark.getWidth();
int wh = waterMark.getHeight();

Bitmap newb = Bitmap.createBitmap(w, h,Bitmap.Config.ARGB_8888;);
Canvas cv = new Canvas(newb);
cv.drawBitmap(originMap, 0, 0, null);
cv.drawBitmap(waterMark, w - ww, h - wh, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();

return newb;
}

谢谢!

最佳答案

这是我用来将水印应用于 jpeg 的代码,它应该也适用于您,

public Bitmap applyWatermarkColorFilter(Drawable drawable) { 
Bitmap image = ((BitmapDrawable)drawable).getBitmap();

Bitmap result = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(image, 0, 0, null);

Bitmap watermark = BitmapFactory.decodeResource(getResources(), R.drawable.watermark);

canvas.drawBitmap(watermark, image.getWidth()/2 - watermark.getWidth()/2,
image.getHeight()/2 - watermark.getHeight()/2,
null);

return result;
}

基本上在此之后你必须使用Bitmap.compress(<arguments>)从中获取jpg。

不要尝试其他格式。如果您可以像我们提取 jpg 和 png 那样从中提取位图,也许就有可能。

关于安卓水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com