[英]Taking a picture via camera intent in Android
I am using the following code for clicking a photo on Button click.我正在使用以下代码单击按钮单击上的照片。
package com.example.clickpic;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
private ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//here,we are making a folder named picFolder to store pics taken by the camera using this application
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1888 && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
The code works perfectly well but it just opens up the camera activity.该代码运行良好,但它只是打开了相机活动。 What must I do so that on button click, it clicks a picture and saves it as well ?
我必须做什么才能在单击按钮时单击图片并将其保存?
Right now, this just opens the camera app of the phone, and expects me to click the picture and click on OK.现在,这只是打开手机的相机应用程序,并希望我单击图片并单击“确定”。 I don't want these things.
我不要这些东西。
All I want is, when I click on the button from my app it should click a picture without expecting any additional input from me.我想要的只是,当我点击应用程序中的按钮时,它应该点击图片,而无需期待我的任何额外输入。
I have already tried to create a camera app of my own, but I ran into some issues.我已经尝试创建自己的相机应用程序,但遇到了一些问题。 That's why I am trying this approach.
这就是我尝试这种方法的原因。 Camera API not working on KITKAT
相机 API 不适用于 KITKAT
All I want is, when I click on the button from my app it should click a picture without expecting any additional input from me.我想要的只是,当我点击应用程序中的按钮时,它应该点击图片,而无需期待我的任何额外输入。 -
——
Its not achieved by Camera Intent like android.provider.MediaStore.ACTION_IMAGE_CAPTURE
.它不是像
android.provider.MediaStore.ACTION_IMAGE_CAPTURE
这样的Camera Intent实现的。 You have to implement camera interface portion in your code without using native camera application of Device.您必须在代码中实现相机接口部分,而无需使用 Device 的本机相机应用程序。
Look at portion of Building Camera App查看构建相机应用程序的一部分
I have written a camera class which takes picture, arranges the orientation (some devices takes photo horizontal as default) and saves the photo taken.我写了一个相机类来拍照,安排方向(有些设备默认水平照片)并保存拍摄的照片。 You can check it from the link below:
您可以从以下链接查看:
Camera capture orientation on samsung devices in android android中三星设备上的相机捕获方向
Edit: Sorry, savePhoto functions are not written in my example.编辑:抱歉,我的示例中没有编写 savePhoto 函数。 Adding them now.
现在添加它们。
savePhoto function:保存照片功能:
public void savePhoto(Bitmap bmp) {
imageFileFolder = new File(Environment.getExternalStorageDirectory(),
cc.getDirectoryName());
imageFileFolder.mkdir();
FileOutputStream out = null;
Calendar c = Calendar.getInstance();
String date = fromInt(c.get(Calendar.MONTH))
+ fromInt(c.get(Calendar.DAY_OF_MONTH))
+ fromInt(c.get(Calendar.YEAR))
+ fromInt(c.get(Calendar.HOUR_OF_DAY))
+ fromInt(c.get(Calendar.MINUTE))
+ fromInt(c.get(Calendar.SECOND));
imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
try {
out = new FileOutputStream(imageFileName);
bmp.compress(Bitmap.CompressFormat.JPEG, 70, out);
out.flush();
out.close();
scanPhoto(imageFileName.toString());
out = null;
} catch (Exception e) {
e.printStackTrace();
}
}
scanPhoto function:扫描照片功能:
public void scanPhoto(final String imageFileName) {
geniusPath = imageFileName;
msConn = new MediaScannerConnection(MyClass.this,
new MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
msConn.scanFile(imageFileName, null);
}
@Override
public void onScanCompleted(String path, Uri uri) {
msConn.disconnect();
}
});
msConn.connect();
}
SavePhotoTask class: SavePhotoTask 类:
class SavePhotoTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... jpeg) {
File photo = new File(Environment.getExternalStorageDirectory(),
"photo.jpg");
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos = new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
} catch (java.io.IOException e) {
}
return (null);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.