Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

Android Taking Photo Capture Screenshots (Camera)

Android Taking Photo Capture Screenshots การถ่ายรูปด้วยการเรียก Camera มาแสดงบน Android App นั้นสามารถทำได้ง่ายมาก ๆ ด้วยการ Intent ไปยัง MediaStore.ACTION_IMAGE_CAPTURE ซึ่งหลังจากที่ Intent จะมีการเรียก App ที่ทำหน้าที่ถ่ายรูปด้วยการเปิดกล้อง (Camera) และ Capture รูปภาพที่ต้องการ แล้วนำกลับมาใช้ยัง App ที่เราเขียนขึ้น โดยเราสามารถนำ Result ที่ได้จากการถ่ายรูปไปใช้งานอื่น ๆ เช่นแสดงผลบน ImageView หรือจัดเก็บลงใน Storage ของ SD Card เพื่อนำไปใช้งานด้านอื่น ๆ

Android Taking Photo Capture Screenshots

Android Taking Photo Capture Screenshots


การเปิดกล้อง Camera ถ่ายรูปและ Capture รูปภาพ
        	  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        	    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        	        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        	    }

หลังจากถ่ายภาพเสร็จแล้วจะ Delegate Method มายัง onActivityResult
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
	        Bundle extras = data.getExtras();
	        Bitmap imageBitmap = (Bitmap) extras.get("data");
	        imgView.setImageBitmap(imageBitmap);
	    }
	}

ในการเรียกใช้งาน Camera จะต้องกำหนด Permission และ Feature ดังนี้

AndroidManifest.xml
	<uses-permission android:name="android.permission.CAMERA" />
 	<uses-feature android:name="android.hardware.camera" android:required="true" />
    <uses-feature android:name="android.hardware.camera.autofocus" />


Example ตัวอย่างการเขียน Android เพื่อเปิดกล้องถ่ายรูปและ Capture รูปภาพ และแสดงผลบน ImageView

Android Taking Photo Capture Screenshots

โครงสร้างของไฟล์ในโปรเจคสามารถใช้ได้ทั้งบน Eclipse และ Android Studio

Android Taking Photo Capture Screenshots

activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
   	<TableRow
      android:id="@+id/tableRow1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
     
     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Camera Take Photo "
        android:layout_span="1"
        android:textAppearance="?android:attr/textAppearanceLarge" />
  	        
 	</TableRow>

	<View
		android:layout_height="1dip"
		android:background="#CCCCCC" />
 
  <TableLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="0.1"
      android:orientation="horizontal" >


      <ImageView
          android:id="@+id/imgView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="0.75"
          android:src="@drawable/ic_launcher" />

      <Button
          android:id="@+id/btnTakePhoto"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Take Photo" />
  </TableLayout>

	<View
		android:layout_height="1dip"
		android:background="#CCCCCC" />
   		  
   	<LinearLayout
      android:id="@+id/LinearLayout1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="5dip" >

   		<TextView
   		    android:id="@+id/textView2"
   		    android:layout_width="wrap_content"
   		    android:layout_height="wrap_content"
   		    android:text="By.. ThaiCreate.Com" />

	</LinearLayout>
	
</TableLayout>









ไฟล์ Java

MainActivity.java
package com.myapp;

import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	
	ImageView imgView;
	static final int REQUEST_IMAGE_CAPTURE = 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //*** ImageView
        imgView = (ImageView) findViewById(R.id.imgView);
        
        //*** Take Photo
        final Button btnTakePhoto = (Button) findViewById(R.id.btnTakePhoto);
        // Perform action on click
        btnTakePhoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        	  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        	    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        	        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        	    }
            }	
        });
        
    }
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
	        Bundle extras = data.getExtras();
	        Bitmap imageBitmap = (Bitmap) extras.get("data");
	        imgView.setImageBitmap(imageBitmap);
	    }
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}









Screenshot

Android Taking Photo Capture Screenshots

หน้าแรกของ App ให้คลิกที่ Take Photo

Android Taking Photo Capture Screenshots

โปรแกรมจะ Intent ไปยัง App ที่ทำหน้าที่ถ่ายรูป และเปิด Camera

Android Taking Photo Capture Screenshots

หลังจากที่ถ่ายรูปเสร็จแล้วโปรแกรมจะถามว่าต้องการ Save รูปนี้หรือไม่

Android Taking Photo Capture Screenshots

เมื่อกด Save ภาพถ่ายจะถูกนำกลับมายัง App และแสดงผลบน ImageView

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2015-11-16 15:55:25 / 2017-03-26 21:06:43
  Download : No files
 Sponsored Links / Related

 
Android Taking Photo and Save Photo to Storage (SD Card)
Rating :

 
Android Taking Photo and Resize Images (Quality, Size, Width, Height)
Rating :

 
Android Taking Photo and Photo to Gallery
Rating :

 
Android Taking Photo and Upload file to Server (PHP/Upload)
Rating :

 
Android Taking Photo and Multiple Upload to Server (PHP/Upload/ProgressDialog)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่