{"id":862,"date":"2023-03-17T01:46:54","date_gmt":"2023-03-16T16:46:54","guid":{"rendered":"https:\/\/www.dogrow.net\/android\/?p=862"},"modified":"2023-03-17T01:48:25","modified_gmt":"2023-03-16T16:48:25","slug":"blog44","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/android\/blog44\/","title":{"rendered":"(44) \u30ab\u30e1\u30e9\u3067\u64ae\u5f71\u3057\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3002"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u81ea\u4f5c\u306e Android\u30a2\u30d7\u30ea\u306e\u4e2d\u3067\u30ab\u30e1\u30e9\u3092\u8d77\u52d5\u3057\u3066\u5199\u771f\u64ae\u5f71\u3057\u3001\u305d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\uff06\u81ea\u7531\u306b\u51e6\u7406\u3057\u305f\u3044\u3002<br \/>\n\u30e1\u30e2\u30ea\u4e0a\u306e Bitmap\u30c7\u30fc\u30bf\u3068\u3057\u3066\u53d6\u5f97\u3067\u304d\u308b\u305d\u3046\u306a\u306e\u3067\u3001\u3053\u308c\u3092\u3084\u3063\u3066\u307f\u305f\u3044\u3002<\/p>\n<h1 class=\"my_h\">2. \u3084\u3063\u3066\u307f\u308b<\/h1>\n<h3 class=\"my_h\">(1) AndroidManifest.xml<\/h3>\n<p>\u30ab\u30e1\u30e9\u306e\u4f7f\u7528\u8a31\u53ef\u8a2d\u5b9a\u3092\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; highlight: [4,5,6,7,8,9]; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\r\n    xmlns:tools=&quot;http:\/\/schemas.android.com\/tools&quot;&gt;\r\n    &lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; \/&gt;\r\n    &lt;queries&gt;\r\n        &lt;intent&gt;\r\n            &lt;action android:name=&quot;android.media.action.IMAGE_CAPTURE&quot; \/&gt;\r\n        &lt;\/intent&gt;\r\n    &lt;\/queries&gt;\r\n    &lt;application\r\n        android:allowBackup=&quot;true&quot;\r\n        android:dataExtractionRules=&quot;@xml\/data_extraction_rules&quot;\r\n<\/pre>\n<h3 class=\"my_h\">(2) MainActivity.java #1 \u30ab\u30e1\u30e9\u8d77\u52d5\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u6642\u306e\u51e6\u7406<\/h3>\n<p>\u203bactResultLauncher \u306f\u5f8c\u8ff0\u3059\u308b\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    public void onPushCameraButton(View view){\r\n        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);\r\n        actResultLauncher.launch(intent);\r\n    }\r\n<\/pre>\n<h3 class=\"my_h\">(3) MainActivity.java #2 \u30ab\u30e1\u30e9\u64ae\u5f71\u5b8c\u4e86\u6642\u306e\u51e6\u7406<\/h3>\n<p>\u203bsaveImage \u306f\u5f8c\u8ff0\u3059\u308b\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    private final ActivityResultLauncher&lt;Intent&gt; actResultLauncher = registerForActivityResult(\r\n            new ActivityResultContracts.StartActivityForResult(),\r\n            result -&gt; {\r\n                if (result.getResultCode() == Activity.RESULT_OK) {\r\n                    Intent data = result.getData();\r\n                    if(data != null) {\r\n                        \/\/ \u64ae\u5f71\u3057\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u3092 Bitmap\u3067\u53d6\u5f97\u3059\u308b\u3002\r\n                        Bundle extras = data.getExtras();\r\n                        bmpImg = (Bitmap)extras.get(&quot;data&quot;);\r\n                        \/\/ \u64ae\u5f71\u3057\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u753b\u9762\u4e0a\u306b\u8868\u793a\u3059\u308b\u3002\r\n                        ImageView iv = findViewById(R.id.iv_pic);\r\n                        iv.setImageBitmap(bmpImg);\r\n                        \/\/ \u64ae\u5f71\u3057\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3059\u308b\u3002\r\n                        saveImage(bmpImg);\r\n                    }\r\n                }\r\n            });\r\n<\/pre>\n<h3 class=\"my_h\">(4) MainActivity.java #3 Bitmap\u30c7\u30fc\u30bf\u3092\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u4fdd\u5b58\u3059\u308b\u51e6\u7406<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    private void saveImage(Bitmap bitmap) {\r\n        \/\/ \u30a2\u30d7\u30ea\u5c02\u7528\u306e\u5185\u90e8\u30b9\u30c8\u30ec\u30fc\u30b8\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u3002\r\n        File appDir = getFilesDir();\r\n        String filePath = String.format(&quot;%s\/myimg.jpg&quot;, appDir.toString());\r\n        \/\/ \u30d5\u30a1\u30a4\u30eb\u30aa\u30fc\u30d7\u30f3\u3057\u3001JPEG\u6700\u9ad8\u54c1\u8cea(100)\u3067\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u51fa\u529b\u3059\u308b\u3002\r\n        try(FileOutputStream fos = new FileOutputStream(new File(filePath))){\r\n            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n<\/pre>\n<h3 class=\"my_h\">(5) MainActivity.java #4 \u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u30ed\u30fc\u30c9\u3057\u3001ImageView\u306b\u8868\u793a\u3059\u308b\u3002<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    private void loadImageToView(){\r\n        \/\/ \u30a2\u30d7\u30ea\u5c02\u7528\u306e\u5185\u90e8\u30b9\u30c8\u30ec\u30fc\u30b8\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u5185\u306e\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u4f5c\u6210\r\n        File appDir = getFilesDir();\r\n        String filePath = String.format(&quot;%s\/myimg.jpg&quot;, appDir.toString());\r\n        \/\/ Bitmap\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8a2d\u5b9a\r\n        BitmapFactory.Options options = new BitmapFactory.Options();\r\n        options.inPreferredConfig = Bitmap.Config.RGB_565;\r\n        \/\/ \u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092 Bitmap\u3067\u8aad\u307f\u51fa\u3059\u3002\r\n        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);\r\n        \/\/ Bitmap\u753b\u50cf\u30c7\u30fc\u30bf\u3092 ImageView\u306b\u8868\u793a\u3059\u308b\u3002\r\n        ImageView iv = findViewById(R.id.iv_pic);\r\n        iv.setImageBitmap(bitmap);\r\n    }\r\n<\/pre>\n<h1 class=\"my_h\">3. \u4ed8\u9332 : \u52d5\u4f5c\u78ba\u8a8d\u6e08\u307f\u306e\u5168\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<\/h1>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.example.testcamera;\r\n\r\nimport androidx.activity.result.ActivityResultLauncher;\r\nimport androidx.activity.result.contract.ActivityResultContracts;\r\nimport androidx.appcompat.app.AppCompatActivity;\r\n\r\nimport android.app.Activity;\r\nimport android.content.Context;\r\nimport android.content.Intent;\r\nimport android.graphics.Bitmap;\r\nimport android.graphics.BitmapFactory;\r\nimport android.os.Bundle;\r\nimport android.provider.MediaStore;\r\nimport android.view.View;\r\nimport android.widget.ImageView;\r\n\r\nimport java.io.File;\r\nimport java.io.FileOutputStream;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n    private Bitmap bmpImg;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n\r\n        loadImageToView();\r\n    }\r\n\r\n    public void onPushCameraButton(View view){\r\n        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);\r\n        actResultLauncher.launch(intent);\r\n    }\r\n\r\n    private final ActivityResultLauncher&lt;Intent&gt; actResultLauncher = registerForActivityResult(\r\n            new ActivityResultContracts.StartActivityForResult(),\r\n            result -&gt; {\r\n                if (result.getResultCode() == Activity.RESULT_OK) {\r\n                    Intent data = result.getData();\r\n                    if(data != null) {\r\n                        \/\/ \u64ae\u5f71\u3057\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u3092 Bitmap\u3067\u53d6\u5f97\u3059\u308b\u3002\r\n                        Bundle extras = data.getExtras();\r\n                        bmpImg = (Bitmap)extras.get(&quot;data&quot;);\r\n                        \/\/ \u64ae\u5f71\u3057\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u753b\u9762\u4e0a\u306b\u8868\u793a\u3059\u308b\u3002\r\n                        ImageView iv = findViewById(R.id.iv_pic);\r\n                        iv.setImageBitmap(bmpImg);\r\n                        \/\/ \u64ae\u5f71\u3057\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3059\u308b\u3002\r\n                        saveImage(bmpImg);\r\n                    }\r\n                }\r\n            });\r\n\r\n    private void saveImage(Bitmap bitmap) {\r\n        \/\/ \u30a2\u30d7\u30ea\u5c02\u7528\u306e\u5185\u90e8\u30b9\u30c8\u30ec\u30fc\u30b8\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u3002\r\n        File appDir = getFilesDir();\r\n        String filePath = String.format(&quot;%s\/myimg.jpg&quot;, appDir.toString());\r\n        \/\/ \u30d5\u30a1\u30a4\u30eb\u30aa\u30fc\u30d7\u30f3\u3057\u3001JPEG\u6700\u9ad8\u54c1\u8cea(100)\u3067\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u51fa\u529b\u3059\u308b\u3002\r\n        try(FileOutputStream fos = new FileOutputStream(new File(filePath))){\r\n            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n\r\n    private void loadImageToView(){\r\n        \/\/ \u30a2\u30d7\u30ea\u5c02\u7528\u306e\u5185\u90e8\u30b9\u30c8\u30ec\u30fc\u30b8\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u5185\u306e\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u4f5c\u6210\r\n        File appDir = getFilesDir();\r\n        String filePath = String.format(&quot;%s\/myimg.jpg&quot;, appDir.toString());\r\n        \/\/ Bitmap\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8a2d\u5b9a\r\n        BitmapFactory.Options options = new BitmapFactory.Options();\r\n        options.inPreferredConfig = Bitmap.Config.RGB_565;\r\n        \/\/ \u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092 Bitmap\u3067\u8aad\u307f\u51fa\u3059\u3002\r\n        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);\r\n        \/\/ Bitmap\u753b\u50cf\u30c7\u30fc\u30bf\u3092 ImageView\u306b\u8868\u793a\u3059\u308b\u3002\r\n        ImageView iv = findViewById(R.id.iv_pic);\r\n        iv.setImageBitmap(bitmap);\r\n    }\r\n}\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 \u81ea\u4f5c\u306e Android\u30a2\u30d7\u30ea\u306e\u4e2d\u3067\u30ab\u30e1\u30e9\u3092\u8d77\u52d5\u3057\u3066\u5199\u771f\u64ae\u5f71\u3057\u3001\u305d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\uff06\u81ea\u7531\u306b\u51e6\u7406\u3057\u305f\u3044\u3002 \u30e1\u30e2\u30ea\u4e0a\u306e Bitmap\u30c7\u30fc\u30bf\u3068\u3057\u3066\u53d6\u5f97\u3067\u304d\u308b\u305d\u3046\u306a\u306e\u3067\u3001\u3053\u308c\u3092\u3084\u3063\u3066\u307f\u305f\u3044\u3002 2. \u3084\u3063\u3066\u307f\u308b (\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/android\/blog44\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-862","post","type-post","status-publish","format-standard","hentry","category-androidstudio"],"views":751,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/comments?post=862"}],"version-history":[{"count":4,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/862\/revisions"}],"predecessor-version":[{"id":867,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/862\/revisions\/867"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/media?parent=862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/categories?post=862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/tags?post=862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}