標題:
MyCamera
[打印本頁]
作者:
ray
時間:
2012-11-30 19:09
標題:
MyCamera
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/Button01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/camera_lbl"
/>
<ListView
android:id="@+id/ListView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-11-30 19:12
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
複製代碼
作者:
ray
時間:
2012-11-30 19:21
private Button takePicBtn;
private ListView myListView;
private ArrayAdapter<String> adapter;
private ArrayList<String> picFiles;
private boolean sdCardExist;
複製代碼
作者:
ray
時間:
2012-11-30 19:27
takePicBtn = (Button)findViewById(R.id.Button01);
myListView = (ListView)findViewById(R.id.ListView01);
sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
複製代碼
作者:
ray
時間:
2012-11-30 19:32
getPicFiles();
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,picFiles);
myListView.setAdapter(adapter);
複製代碼
作者:
ray
時間:
2012-11-30 19:38
private void getPicFiles()
{
picFiles = new ArrayList<String>();
if(sdCardExist)
{
}
}
複製代碼
作者:
ray
時間:
2012-11-30 19:45
if(sdCardExist)
{
File myDir = Environment.getExternalStorageDirectory();
File[] files = myDir.listFiles();
if(files != null)
{
for(int i = 0 ; i < files.length;i++)
{
if(files[i].isFile() && files[i].getName().substring(files[i].getName().indexOf(".")).toLowerCase().equals(".jpg"))
{
}
}
}
}
複製代碼
作者:
ray
時間:
2012-11-30 19:52
takePicBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
複製代碼
作者:
ray
時間:
2012-11-30 19:58
if(v == takePicBtn)
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
複製代碼
作者:
ray
時間:
2012-11-30 20:07
try
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File myDir = Environment.getExternalStorageDirectory();
File myFile = File.createTempFile("MyCamera_", ".jpg", myDir);
Uri outputFile = Uri.fromFile(myFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
startActivity(intent);
}
catch(Exception e)
{}
複製代碼
作者:
ray
時間:
2012-11-30 20:14
package com.example.mycamera;
import java.io.File;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity
{
private Button takePicBtn;
private ListView myListView;
private ArrayAdapter<String> adapter;
private ArrayList<String> picFiles;
private boolean sdCardExist;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePicBtn = (Button)findViewById(R.id.Button01);
myListView = (ListView)findViewById(R.id.ListView01);
sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
getPicFiles();
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,picFiles);
myListView.setAdapter(adapter);
takePicBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
if(v == takePicBtn)
{
try
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File myDir = Environment.getExternalStorageDirectory();
File myFile = File.createTempFile("MyCamera_", ".jpg", myDir);
Uri outputFile = Uri.fromFile(myFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
startActivity(intent);
}
catch(Exception e)
{}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void getPicFiles()
{
picFiles = new ArrayList<String>();
if(sdCardExist)
{
File myDir = Environment.getExternalStorageDirectory();
File[] files = myDir.listFiles();
if(files != null)
{
for(int i = 0 ; i < files.length;i++)
{
if(files[i].isFile() && files[i].getName().substring(files[i].getName().indexOf(".")).toLowerCase().equals(".jpg"))
{
picFiles.add(files[i].getName());
}
}
}
}
}
}
複製代碼
作者:
ray
時間:
2012-11-30 20:41
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/Button01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/camera_lbl"
/>
<ListView
android:id="@+id/ListView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-11-30 20:49
package com.example.mycamera;
import java.io.File;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity
{
private Button takePicBtn;
private ListView myListView;
private ArrayAdapter<String> adapter;
private ArrayList<String> picFiles;
private boolean sdCardExist;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePicBtn = (Button)findViewById(R.id.Button01);
myListView = (ListView)findViewById(R.id.ListView01);
sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
getPicFiles();
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,picFiles);
myListView.setAdapter(adapter);
takePicBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
if(v == takePicBtn)
{
try
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File myDir = Environment.getExternalStorageDirectory();
File myFile = File.createTempFile("MyCamera_", ".jpg", myDir);
Uri outputFile = Uri.fromFile(myFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
startActivity(intent);
}
catch(Exception e)
{}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onResume()
{
super.onResume();
sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
getPicFiles();
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,picFiles);
myListView.setAdapter(adapter);
}
private void getPicFiles()
{
picFiles = new ArrayList<String>();
if(sdCardExist)
{
File myDir = Environment.getExternalStorageDirectory();
File[] files = myDir.listFiles();
if(files != null)
{
for(int i = 0 ; i < files.length;i++)
{
if(files[i].isFile() && files[i].getName().substring(files[i].getName().indexOf(".")).toLowerCase().equals(".jpg"))
{
picFiles.add(files[i].getName());
}
}
}
}
}
}
複製代碼
作者:
ray
時間:
2012-11-30 20:58
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:id="@+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/camera_lbl">
</Button>
<SurfaceView android:id="@+id/SurfaceView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</SurfaceView>
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-11-30 20:59
package com.example.mycamera;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class CameraActivity extends Activity
{
private static final String TAG = "MyCamera";
final int latchedOrientation = 90;
private Camera mCamera;
private String picFileName;
private boolean sdCardExist; //SD Card是否存在
private String strTempFile = "MyCamera_"; //前置檔名
// Create surface for preview
private SurfaceHolder.Callback mSurfaceListener =
new SurfaceHolder.Callback()
{
// Surface create
public void surfaceCreated(SurfaceHolder holder)
{
mCamera = Camera.open();
try
{
mCamera.setPreviewDisplay(holder);
}
catch (Exception e)
{
e.printStackTrace();
}
}
// Surface destroy
public void surfaceDestroyed(SurfaceHolder holder)
{
mCamera.release();
mCamera = null;
}
// Surface change
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("jpeg-quality", 85);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
};
// Auto focus to take a photo
private Camera.AutoFocusCallback mAutoFocusListener =
new Camera.AutoFocusCallback()
{
public void onAutoFocus(boolean success, final Camera camera)
{
Log.i(TAG,"AutoFocus : " + success);
camera.autoFocus(null);
camera.takePicture(mShutterListener, null, mPictureListener);
new Thread()
{
@Override
public void run()
{
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
camera.startPreview();
Intent intent = new Intent ();
intent.putExtra ("PIC_FILENAME", picFileName);
setResult (-1, intent);
finish ();
}
}.start();
}
};
// Listener for Shut process
private Camera.ShutterCallback mShutterListener =
new Camera.ShutterCallback() {
public void onShutter() {
Log.i(TAG, "onShutter");
}
};
// Listener for Picture process
private Camera.PictureCallback mPictureListener =
new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.i(TAG, "Picture taken");
FileOutputStream fos = null;
try
{
/* 判斷SD Card是否插入 */
sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
/* 取得SD Card路徑做為錄音的檔案位置 */
if (sdCardExist)
{
File myDir = Environment.getExternalStorageDirectory();
File myFile = File.createTempFile(strTempFile, ".jpg",myDir);
picFileName = myFile.getName();
String myFileName = myFile.getPath();
fos = new FileOutputStream(myFileName);
fos.write(data);
fos.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
// Press Camera photo button
private View.OnClickListener mButtonListener = new View.OnClickListener()
{
public void onClick(View v)
{
if(mCamera != null)
{
mCamera.autoFocus(mAutoFocusListener);
}
}
};
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// Camera preview
SurfaceView surface = (SurfaceView)findViewById(R.id.SurfaceView01);
SurfaceHolder holder = surface.getHolder();
holder.addCallback(mSurfaceListener);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// Camera photo button
Button button = (Button)findViewById(R.id.ImageButton01);
button.setOnClickListener(mButtonListener);
}
}
複製代碼
作者:
ray
時間:
2012-11-30 21:11
package com.example.mycamera;
import java.io.File;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity
{
private Button takePicBtn;
private ListView myListView;
private ArrayAdapter<String> adapter;
private ArrayList<String> picFiles;
private boolean sdCardExist;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePicBtn = (Button)findViewById(R.id.Button01);
myListView = (ListView)findViewById(R.id.ListView01);
sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
getPicFiles();
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,picFiles);
myListView.setAdapter(adapter);
takePicBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
if(v == takePicBtn)
{
Intent intent = new Intent(MainActivity.this,CameraActivity.class);
startActivityForResult(intent,1);
/*
try
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File myDir = Environment.getExternalStorageDirectory();
File myFile = File.createTempFile("MyCamera_", ".jpg", myDir);
Uri outputFile = Uri.fromFile(myFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
startActivity(intent);
}
catch(Exception e)
{}
*/
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/*
@Override
protected void onResume()
{
super.onResume();
sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
getPicFiles();
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,picFiles);
myListView.setAdapter(adapter);
}
*/
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1)
{
if(resultCode == RESULT_OK)
{
String picName = data.getStringExtra("PIC_FILENAME");
adapter.add(picName);
}
}
}
private void getPicFiles()
{
picFiles = new ArrayList<String>();
if(sdCardExist)
{
File myDir = Environment.getExternalStorageDirectory();
File[] files = myDir.listFiles();
if(files != null)
{
for(int i = 0 ; i < files.length;i++)
{
if(files[i].isFile() && files[i].getName().substring(files[i].getName().indexOf(".")).toLowerCase().equals(".jpg"))
{
picFiles.add(files[i].getName());
}
}
}
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2