{"id":5387,"date":"2021-11-04T11:09:07","date_gmt":"2021-11-04T03:09:07","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress\/?p=5387"},"modified":"2025-02-14T21:56:43","modified_gmt":"2025-02-14T13:56:43","slug":"bitmap-api","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=5387","title":{"rendered":"Bitmap"},"content":{"rendered":"<h1>Bitmap \u662f\u4ec0\u4e48<\/h1>\n<p><a href=\"https:\/\/juejin.cn\/post\/7439621582617722931\">https:\/\/juejin.cn\/post\/7439621582617722931<\/a><\/p>\n<h1>Bitmap API<\/h1>\n<p><a href=\"http:\/\/www.cnblogs.com\/liyan-blogs\/p\/5535071.html\">http:\/\/www.cnblogs.com\/liyan-blogs\/p\/5535071.html<\/a><\/p>\n<ul>\n<li>\n<p>\u6587\u4ef6\u8f6cbitmap<br \/>\n<a href=\"https:\/\/blog.51cto.com\/u_16175437\/13089042\">https:\/\/blog.51cto.com\/u_16175437\/13089042<\/a><\/p>\n<\/li>\n<li>\n<p>ByteArray\u8f6c\u6210\u56fe\u7247<br \/>\n<a href=\"https:\/\/blog.51cto.com\/u_16213324\/8764082\">https:\/\/blog.51cto.com\/u_16213324\/8764082<\/a><\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ \u5c06ByteArray\u8f6c\u6362\u4e3aBitmap\npublic Bitmap byteArrayToBitmap(byte[] byteArray) {\n    return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);\n}\n\n\/\/ \u5c06Bitmap\u4fdd\u5b58\u4e3a\u672c\u5730\u56fe\u7247\u6587\u4ef6\npublic void saveBitmapToFile(Bitmap bitmap, String filePath) {\n    File file = new File(filePath);\n    FileOutputStream fos;\n    try {\n        fos = new FileOutputStream(file);\n        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);\n        fos.flush();\n        fos.close();\n    } catch (IOException e) {\n        e.printStackTrace();\n    }\n}\n\n\/\/ \u793a\u4f8b\u7528\u6cd5\nbyte[] byteArray = \/\/ \u4ece\u670d\u52a1\u5668\u83b7\u53d6\u7684\u5b57\u8282\u6570\u7ec4\nBitmap bitmap = byteArrayToBitmap(byteArray);\nsaveBitmapToFile(bitmap, &quot;\/sdcard\/image.jpg&quot;);<\/code><\/pre>\n<ul>\n<li>\u6307\u5b9a\u4e00\u4e2a\u8272\u503c\u751f\u6210bitmap<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public Bitmap getBackGroundBitmap(int color) {\n\n                     Paint p = new Paint();\n\n                     p.setColor(Color.RED);\n\n                     Bitmap bitmap = Bitmap.createBitmap(190, 110, Bitmap.Config.ARGB_8888);\n\n                     Canvas canvas = new Canvas(bitmap);\n\n                     canvas.drawColor(android.R.color.transparent);\n\n                     canvas.drawColor(color);\n\n                     return bitmap;\n\n       }<\/code><\/pre>\n<ul>\n<li>\u8bbe\u7f6ebitmap\u7684\u900f\u660e\u5ea6<\/li>\n<\/ul>\n<pre><code class=\"language-java\"> public static Bitmap getAlplaBitmap(Bitmap sourceImg, int number) {\n\n        \u3000\u3000int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];\n\n        \u3000\u3000sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg.getWidth(), sourceImg.getHeight());\n\n        \u3000\u3000number = number * 255 \/ 100;\n\n        \u3000\u3000for (int i = 0; i &lt; argb.length; i++) {\n\n              \u3000\u3000argb[i] = (number &lt;&lt; 24) | (argb[i] &amp; 0x00FFFFFF)\uff1b\n\n        \u3000\u3000}\n\n         \u3000\u3000sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);\n\n        return sourceImg;\n\n\u3000\u3000 }<\/code><\/pre>\n<ul>\n<li>drawable\u8f6c\u6362\u6210bitmap<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\u3000\u3000 public static Bitmap drawableToBitmap(Drawable drawable) {\n\n       \u3000\u3000 Bitmap bitmap = Bitmap.createBitmap(\n\n               \u3000\u3000 drawable.getIntrinsicWidth(),\n\n               \u3000\u3000 drawable.getIntrinsicHeight(),\n\n               \u3000\u3000 drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);\n\n       \u3000\u3000 Canvas canvas = new Canvas(bitmap);\n\n       \u3000\u3000 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());\n\n       \u3000\u3000 drawable.draw(canvas);\n\n       \u3000\u3000 return bitmap\uff1b\n\n\u3000\u3000 }<\/code><\/pre>\n<ul>\n<li>bitmap\u5706\u89d2<\/li>\n<\/ul>\n<pre><code class=\"language-java\"> \u3000\u3000public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {\n\n       \u3000\u3000  Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),\n\n               bitmap.getHeight(), Config.ARGB_8888);\n\n        \u3000\u3000 Canvas canvas = new Canvas(output);\n\n        \u3000\u3000 final int color = Color.RED; \n\n        \u3000\u3000 final Paint paint = new Paint();\n\n        \u3000\u3000 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());\n\n        \u3000\u3000 final RectF rectF = new RectF(rect);\n\n        \u3000\u3000 final float roundPx = 6\uff1b\n\n         \u3000\u3000paint.setAntiAlias(true);\n\n         \u3000\u3000canvas.drawARGB(0, 0, 0, 0);\n\n         \u3000\u3000paint.setColor(color);\n\n         \u3000\u3000canvas.drawRoundRect(rectF, roundPx, roundPx, paint);\n\n         \u3000\u3000paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));\n\n         \u3000\u3000canvas.drawBitmap(bitmap, rect, rect, paint); \n\n        \u3000\u3000 return output;\n\n\u3000\u3000 }<\/code><\/pre>\n<button class=\"simplefavorite-button\" data-postid=\"5387\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>Bitmap \u662f\u4ec0\u4e48 https:\/\/juejin.cn\/post\/7439621582617722 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[323],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5387"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5387"}],"version-history":[{"count":5,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5387\/revisions"}],"predecessor-version":[{"id":15555,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5387\/revisions\/15555"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5387"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}