我已經提出了一個應用程序來捕捉相機的照片。如何從Android攝像頭捕捉圖像?
我已經創建了兩個活動:在Activity1中有一個Button,點擊時啓動相機。當圖像被捕獲時,它被傳遞給Activity2。
但是,當我運行應用程序並啓動Activity1(使用一個按鈕),然後單擊按鈕啓動攝像頭時,它會彈出一個窗口,顯示消息「不幸,攝像頭已停止」。日誌貓或控制檯中沒有錯誤。
任何人都可以幫助我。請。非常感謝。
我已經提出了一個應用程序來捕捉相機的照片。如何從Android攝像頭捕捉圖像?
我已經創建了兩個活動:在Activity1中有一個Button,點擊時啓動相機。當圖像被捕獲時,它被傳遞給Activity2。
但是,當我運行應用程序並啓動Activity1(使用一個按鈕),然後單擊按鈕啓動攝像頭時,它會彈出一個窗口,顯示消息「不幸,攝像頭已停止」。日誌貓或控制檯中沒有錯誤。
任何人都可以幫助我。請。非常感謝。
看看這篇博客寫我正好在這個題目:
Use Camera Activity for Thumbnail and Full Size Image
它可以幫助你找出你的問題。
的步驟是:
所以首先像以前一樣,我們需要創建一個靜態INT這將是我們
public static final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;
2.接下來我們火的意圖開始對於結果的活動:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);
在這裏,我們實際上是傳遞一個URI作爲一個額外的意圖,以便將圖像保存在這個位置,當它將被採取。
最後,我們將得到的結果onActivityResult
:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//Check that request code matches ours:
if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE)
{
//Get our saved file into a bitmap object:
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
}
}
當decodeSampledBitmapFromFile
方法是:
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
{ // BEST QUALITY MATCH
//First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize, Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight)
{
inSampleSize = Math.round((float)height/(float)reqHeight);
}
int expectedWidth = width/inSampleSize;
if (expectedWidth > reqWidth)
{
//if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width/(float)reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
不要忘記將培訓相關相機權限添加到清單文件:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
確保添加權限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果不解決問題的youre妳比應該告訴我們您選擇的代碼
你,如果你使用startActiviyForResult()只需要一個活動:
貝洛,一個簡單的源代碼用於從相機應用拍攝照片,您需要使用startActiviyForResult()啓動Activity並從相機應用程序接收該意圖。
Java源代碼:
package com.example.coursandroid_chp;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MediaActivity extends Activity {
private static final String TAG = "MediaActivity";
private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int REQUEST_VIDEO_CAPTURE = 2;
private Button mCameraPhotoButton;
private Button mCameraVideoButton;
private ImageView mPhotoImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_media);
mCameraPhotoButton = (Button) this.findViewById(R.id.screen_media_camera_photo_button);
mCameraVideoButton = (Button) this.findViewById(R.id.screen_media_camera_video_button);
mPhotoImageView = (ImageView) this.findViewById(R.id.screen_media_photo_imageview);
mCameraPhotoButton.setOnClickListener(onClickListener);
mCameraVideoButton.setOnClickListener(onClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.screen_media, menu);
return true;
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.screen_media_camera_photo_button:
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), REQUEST_IMAGE_CAPTURE);
break;
case R.id.screen_media_camera_video_button:
startActivityForResult(new Intent(MediaStore.ACTION_VIDEO_CAPTURE), REQUEST_VIDEO_CAPTURE);
break;
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
switch (resultCode) {
case RESULT_OK:
Log.v(TAG, "Picture taken! :)");
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
mPhotoImageView.setImageBitmap(bitmap);
}
break;
case RESULT_CANCELED:
Log.v(TAG, "Picture canceled! :(");
break;
}
} else if (requestCode == REQUEST_VIDEO_CAPTURE) {
switch (resultCode) {
case RESULT_OK:
Log.v(TAG, "Video taken! :)");
break;
case RESULT_CANCELED:
Log.v(TAG, "Video canceled! :(");
break;
}
}
}
}
Xml layout file
<RelativeLayout 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"
tools:context=".MediaActivity" >
<Button
android:id="@+id/screen_media_camera_photo_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Camera photo" />
<Button
android:id="@+id/screen_media_camera_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/screen_media_camera_photo_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Camera video" />
<ImageView
android:id="@+id/screen_media_photo_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/screen_media_camera_video_button"
android:layout_centerHorizontal="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="57dp"
android:src="@drawable/ic_bitmap" />
</RelativeLayout>
試試下面的代碼:
package com.example.sample1;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CapturePhotoSample1 extends Activity implements OnClickListener
{
public static final int TAKE_PHOTO=1;
ImageView imageView=null;
private File folder;
String imageFileName=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_capture_photo_sample1);
Button button=(Button)this.findViewById(R.id.capture_button);
button.setOnClickListener(this);
button=null;
imageView=(ImageView)this.findViewById(R.id.image_view1);
folder = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES)+ "/sample1/");
if (!folder.exists()) {
folder.mkdir();
}
}
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent,actionCode);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
if(id==R.id.capture_button)
this.dispatchTakePictureIntent(TAKE_PHOTO);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}
public void handleCameraPhoto(Intent intent)
{
Bundle extras=intent.getExtras();
Bitmap bitmap=(Bitmap)extras.get("data");
this.imageView.setImageBitmap(bitmap);
if(this.isExternalStorageAvailable())
{
this.imageFileName="img_"+SDUtil.now(-1)+".png";
/*SDUtil.now() is our own library.It is for creating file name with respect to data and time.IF u copy the hole program means sdutil shows error.For that you write a logic for creating a file name. */
String path=folder+"/"+this.imageFileName;
FileOutputStream fos=null;
BufferedOutputStream bos=null;
try
{
fos=new FileOutputStream(path);
bos=new BufferedOutputStream(fos);
bitmap.compress(Bitmap.CompressFormat.PNG, 40, bos);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(bos!=null)
{
try
{
bos.flush();
//bos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
if(bos!=null)
{
try
{
bos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
if(fos!=null)
{
try
{
fos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
bos=null;
fos=null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==Activity.RESULT_OK)
{
if(requestCode==TAKE_PHOTO)
{
handleCameraPhoto(data);
}
}
}
private boolean isExternalStorageAvailable() {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
.getPath());
double sdAvailSize = (double) stat.getAvailableBlocks()
* (double) stat.getBlockSize();
// One binary gigabyte equals 1,073,741,824 bytes.
double mbAvailable = sdAvailSize/1048576;
String state = Environment.getExternalStorageState();
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if (mExternalStorageAvailable == true
&& mExternalStorageWriteable == true && mbAvailable > 10) {
return true;
} else {
return false;
}
}
}
此行顯示錯誤(import com.ivy.lib.SDUtil;)。如何導入SDTUtil? – TJM
Tazeen我更新了我的答案,現在檢查它 –
檢查下面的代碼在你的活動
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//Check that request code matches ours:
if (requestCode == CAPTURE_IMAGE_THUMBNAIL_ACTIVITY_REQUEST_CODE)
{ //Check if your application folder exists in the external storage, if not create it:
your code should be here.....
//Check if data in not null and extract the Bitmap:
if (data != null)
{
String filename = "image";
String fileNameExtension = ".jpg";
File sdCard = Environment.getExternalStorageDirectory();
String imageStorageFolder = File.separator+"Your application Folder"+File.separator;
File destinationFile = new File(sdCard, imageStorageFolder + filename + fileNameExtension);
Log.d(TAG, "the destination for image file is: " + destinationFile);
if (data.getExtras() != null)
{
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
try
{
FileOutputStream out = new FileOutputStream(destinationFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
catch (Exception e)
{
Log.e(TAG, "ERROR:" + e.toString());
}
}}}
它不需要任何權限,如果他調用本機相機應用。拍攝照片的是相機,而不是自己的應用程序。如果您想在相機api上訪問,則需要相機權限。 – Jarvis
攝像頭許可他不需要,但是我自己正在使用攝像頭應用程序,並且我發現了一些帖子,他們說由於某些設備的問題而添加權限。從來沒有可以測試這個,但只是添加它是肯定的。他需要能夠保存他的圖像,不是嗎? –