bitmap

微信分享报错 checkArgs fail, thumbData is invalid (bitmap压缩)

天涯浪子 提交于 2020-01-11 10:43:33
概述 近期使用微信分享碰到报错: checkArgs fail, thumbData is invalid 查阅文档后发现是因为分享缩略图的大小不可超过32kb。 于是在框架里加入了压缩图片的逻辑。 源码 压缩逻辑很简单:查看数据是否超过限制,如果超过就将bitmap质量再减一半。 /** * Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb * @param bitmap * @param maxkb * @return */ public static byte [ ] bmpToByteArray ( Bitmap bitmap , final boolean needRecycle , int maxkb ) { if ( bitmap == null ) { return null ; } try { ByteArrayOutputStream output = new ByteArrayOutputStream ( ) ; byte [ ] result = null ; bitmap . compress ( Bitmap . CompressFormat . PNG , 100 , output ) ; int options = 100 ; while ( output . toByteArray ( ) . length > maxkb ) {

Bitmap 位图 Java实现

£可爱£侵袭症+ 提交于 2020-01-11 09:15:32
一、结构思想 以 bit 作为存储单位进行 0、1存取的数据结构。 可用作布尔值存取,比如给定第i位,该bit为1则表示true,为0则表示false。 二、使用场景及优点 适用于对布尔或0、1值进行(大量)存取的场景。 如:记录一个用户365天的签到记录,签了为true,没签为false。若是以普通key/value数据结构,每个用户都需要记录365条,当用户量很大时会造成巨大的空间开销。 因此运用位图的话,每天签到记录只占1个位(bit),一共就365位,则只需48个字节就能容纳一个用户一年的签到记录。 优点 : 低空间开销且高效的0、1存取方案 三、具体实现 实现源码: https://github.com/SimpleIto/data_structure/blob/master/src/bitmap/Bitmap.java 主要考虑以下问题: 用什么物理结构存储一系列bit? 如何通过位操作高效的实现对指定bit的获取、修改操作?(而不是通过字符串转去转来臃肿的实现) 解决思路: Java中,使用 byte[] 字节数组来存储bit。 1 byte = 8 bit 对于获取操作 思路:拿到目标bit所在的byte后,将其向右位移(并将高位置0),使目标bit在第一位,这样结果值就是目标bit值。 1) 通过 byte[index >> 3] (等价于byte[index/8

Convert RGB8 byte[] to Bitmap

守給你的承諾、 提交于 2020-01-11 08:39:44
问题 I have raw pixel data coming from a camera in RGB8 format which I need to convert to a Bitmap . However, the Bitmap PixelFormat only seems to support RGB 16, 24, 32, and 48 formats. I attempted to use PixelFormat.Format8bppIndexed , but the image appears discolored and inverted. public static Bitmap CopyDataToBitmap(byte[] data) { var bmp = new Bitmap(640, 480, PixelFormat.Format8bppIndexed); var bmpData = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp

How to fix “canvas: trying to use a recycled bitmap error”?

拥有回忆 提交于 2020-01-11 08:37:07
问题 I'm creating a RecyclerView to show a Grid of pictures. When selecting one of them, it should open a new activity with a transition. I'm using Glide library to load the pictures and the transition looks awful because it reloads the picture in the new activity. So I had to save it in cache, and then use it for the transition. I have the code, but sometimes if the picture doesn't load, it throws a Canvas RuntimeException. This is the log: 07-03 15:19:58.633 28461-28461/jahirfiquitiva.project E

Resize bitmap inside a drawable layer-list

99封情书 提交于 2020-01-11 05:30:35
问题 Currently I'm developing a Android application. I want to make a text (a header) with a background that looks like this: _______ /______/ I have the following code: <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <gradient android:startColor="@color/jaarkleur" android:centerColor="@color/jaarkleur" android:endColor="#E66C2C00" android:angle="0" /> <stroke android:width="1dp" android:color="#ff000000" /> </shape> <

C# Load JPG file, extract BitmapImage

为君一笑 提交于 2020-01-11 04:26:07
问题 I am trying to extract a BitmapImage from a JPG. This is the code I have: FileStream fIn = new FileStream(sourceFileName, FileMode.Open); // source JPG Bitmap dImg = new Bitmap(fIn); MemoryStream ms = new MemoryStream(); dImg.Save(ms, ImageFormat.Jpeg); image = new BitmapImage(); image.BeginInit(); image.StreamSource = new MemoryStream(ms.ToArray()); image.EndInit(); ms.Close(); image comes back with a 0 × 0 image, which of course means it didn't work. How do I do this? 回答1: Try this: public

C#和Python 图片和base64的互转

China☆狼群 提交于 2020-01-11 03:06:15
C#实例代码: /// <summary> /// 图片转base64 /// </summary> /// <param name="bmp"></param> /// <returns></returns> private string ImgToByte(Bitmap bmp) { string result; try { //Image bmp = new Bitmap(filename); MemoryStream memoryStream = new MemoryStream(); bmp.Save(memoryStream, ImageFormat.Jpeg); byte[] array = new byte[memoryStream.Length]; memoryStream.Position = 0L; memoryStream.Read(array, 0, (int)memoryStream.Length); memoryStream.Close(); result = Convert.ToBase64String(array); } catch (Exception ex) { result = null; } return result; } /// <summary> /// base64转图片 /// </summary> /// <param name

创建缩略图

情到浓时终转凉″ 提交于 2020-01-11 00:53:44
string file = files[i]; Image img = Image.FromFile(file); int width = img.Width; int height = img.Height; if (width > height) { width = 800 ; height = width * img.Height / img.Width; } else { height = 800 ; width = height * img.Width / img.Height; } Bitmap bt = new Bitmap(width, height); Graphics g = Graphics.FromImage(bt); g.DrawImage(img, 0 , 0 , width, height); // g.Save( // Image small = img.GetThumbnailImage(width, height, null, IntPtr.Zero); bt.Save( string .Format( " {0}m{1}.jpg " , topath, i.ToString( " 0000 " )), ImageFormat.Jpeg); 来源: https://www.cnblogs.com/gateluck/archive/2011/07

C#创建不规则窗体四种方式 01

谁说我不能喝 提交于 2020-01-10 23:48:27
现在,C#创建不规则窗体不是一件难事,下面总结一下:    一、自定义窗体,一般为规则的图形,如圆、椭圆等。 做法:重写Form1_Paint事件(Form1是窗体的名字),最简单的一种情况如下: System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath(); shape.AddEllipse( 0 , 0 ,this.Height, this.Width); this.Region = new Region(shape);   做法:重写Form1_Paint事件(Form1是窗体的名字),最简单的一种情况如下:   即重绘窗体的规则。    二、利用背景图片实现   1. 设置窗体的背景图片,其中背景图片是24位(不包括24)以下的位图(BMP图片),并且要设置TansparencyKey的值,一般为你背景图片的背景色,即创建不规则图片时的底色,一般设为你图片中没有的颜色。   这种做法的不好的地方就是背景图片一定要16位或者更低的,而且还要确保客户端的显示。如果监视器的颜色深度设置大于 24 位,则不管 TransparencyKey 属性是如何设置的,窗体的非透明部分都会产生显示问题。若要避免出现这种问题,请确保“显示”控制面板中的监视器颜色深度的设置小于

一起谈.NET技术,C#创建不规则窗体的几种方式

不问归期 提交于 2020-01-10 23:48:02
现在, C# 创建不规则窗体不是一件难事,下面总结一下 : 一、 自定义窗体,一般为规则的图形,如圆、椭圆等。 做法:重写 Form1_Paint 事件( Form1 是窗体的名字),最简单的一种情况如下: System.Drawing.Drawing2D. GraphicsPath shape = new System.Drawing.Drawing2D. GraphicsPath (); shape.AddEllipse(0,0, this .Height, this .Width); this .Region = new Region (shape); 即重绘窗体的规则。 二、利用背景图片实现 1. 设置窗体的背景图片,其中背景图片是 24 位(不包括 24 )以下的位图( BMP 图片),并且要设置 TansparencyKey 的值,一般为你背景图片的背景色,即创建不规则图片时的底色,一般设为你图片中没有的颜色。 这种做法的不好的地方就是背景图片一定要 16 位或者更低的,而且还要确保客户端的显示。如果监视器的颜色深度设置大于 24 位,则不管 TransparencyKey 属性是如何设置的,窗体的非透明部分都会产生显示问题。若要避免出现这种问题,请确保“显示”控制面板中的监视器颜色深度的设置小于 24 位。当开发具有这种透明功能的应用程序时