{"id":1311,"date":"2021-04-04T17:46:27","date_gmt":"2021-04-04T09:46:27","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1311"},"modified":"2021-07-28T09:21:05","modified_gmt":"2021-07-28T01:21:05","slug":"android-bitmap%e8%bd%89file-file%e8%bd%89bitmap-%e5%a3%93%e7%b8%ae%e6%97%8b%e8%bd%89","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-bitmap%e8%bd%89file-file%e8%bd%89bitmap-%e5%a3%93%e7%b8%ae%e6%97%8b%e8%bd%89\/","title":{"rendered":"Android Bitmap\u8f49File File\u8f49Bitmap \u58d3\u7e2e&#038;\u65cb\u8f49"},"content":{"rendered":"<h1>Android Bitmap\u8f49File File\u8f49Bitmap \u58d3\u7e2e&#038;\u65cb\u8f49<\/h1>\n<ol>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-bitmap\u8f49file-file\u8f49bitmap-\u58d3\u7e2e\u65cb\u8f49\/fly\/android\/#a\">\u5c07Bitmap\u8f49\u70baFile<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-bitmap\u8f49file-file\u8f49bitmap-\u58d3\u7e2e\u65cb\u8f49\/fly\/android\/#b\">\u8def\u5f91\u53d6\u5f97File<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-bitmap\u8f49file-file\u8f49bitmap-\u58d3\u7e2e\u65cb\u8f49\/fly\/android\/#c\">\u7528\u8cea\u91cf\u58d3\u7e2e\u5716\u7247(2M)<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/fly\/android-bitmap\u8f49file-file\u8f49bitmap-\u58d3\u7e2e\u65cb\u8f49\/fly\/android\/#d\">\u5c07\u5716\u7247\u89d2\u5ea6\u65cb\u8f49\u70ba\u6b63<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h3>1.\u5c07Bitmap\u8f49\u70baFile<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">val bitmap = BitmapFactory.decodeResource(resources, R.drawable.girl)\nval file = File(externalCacheDir, \"girl.jpg\")\nval ops = FileOutputStream(file)\nbitmap.compress(Bitmap.CompressFormat.JPEG, 100, ops)\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h3>2.\u8def\u5f91\u53d6\u5f97File<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">val file = File(externalCacheDir, \"girl.jpg\")\ncompressFile(file)\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h3>3.\u7528\u8cea\u91cf\u58d3\u7e2e\u5716\u7247(2M)<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">private fun compressFile(file: File) {\n    \/\/2M\n    val twoMB = 1024 * 1024 * 2\n    var quality = 100\n    if (file.exists()) {\n        while (file.length() > twoMB) {\n            quality -= 5\n            val bitmap = BitmapFactory.decodeFile(file.absolutePath)\n            val matrix = Matrix()\n            \/\/\u628a\u89d2\u5ea6\u8f49\u6b63\n            matrix.setRotate(readPictureDegree(file.absolutePath))\n            val createBitmap =\n                Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)\n            val out = FileOutputStream(File(file.absolutePath))\n            createBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)\n            out.flush()\n            out.close()\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h3>4.\u5c07\u5716\u7247\u89d2\u5ea6\u65cb\u8f49\u70ba\u6b63<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">private fun readPictureDegree(path: String): Float {\n    var degree = 0f\n    val exifInterface = ExifInterface(path)\n    val orientation = exifInterface.getAttributeInt(\n        ExifInterface.TAG_ORIENTATION,\n        ExifInterface.ORIENTATION_NORMAL\n    )\n    when (orientation) {\n        ExifInterface.ORIENTATION_ROTATE_90 -> degree = 90f\n        ExifInterface.ORIENTATION_ROTATE_180 -> degree = 180f\n        ExifInterface.ORIENTATION_ROTATE_270 -> degree = 270f\n    }\n    return degree\n}\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Android Bitmap\u8f49File File\u8f49Bitmap \u58d3\u7e2e&#038;\u65cb\u8f49 \u5c07Bitmap\u8f49\u70baFil &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,23,15],"class_list":["post-1311","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-bitmap","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1311"}],"version-history":[{"count":5,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1311\/revisions"}],"predecessor-version":[{"id":1450,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1311\/revisions\/1450"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}