在我的Android项目中,我想用相机拍摄照片,但不要将其保存为文件,只能将其显示在ImageView 上,作为drawable或其他。
在我的项目中,我可以看到相机,如下面的代码。
我尝试了很多方法,但我不能拍照而不保存SDCard。
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holdMe;
private Camera theCamera;
public ShowCamera(Context context, Camera camera) {
super(context);
theCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e) {
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
}
public class MainActivity extends Activity {
private Camera cameraObject;
private ShowCamera showCamera;
public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
}
catch (Exception e){
}
return object;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
谢谢!
答案 0 :(得分:0)
首先,不要忘记将它放在AndroidManifest.xml中:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
因此,您可以按照以下示例操作:http://hmkcode.com/android-camera-taking-photos-camera/
答案 1 :(得分:0)
您需要在此按钮点击例程中包含此行:
mCamera.takePicture(null,null,mPicture);
然后创建例程:
private PictureCallback mPicture = new PictureCallback(){ //+ a lot of things...}
下面的代码应该在PictureCallBack例程
中public void onPictureTaken(byte[] data, Camera camera){
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
try{
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}
catch(FileNotFoundException e){}
catch(IOException e){}
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
}
并尝试找到该功能:
private static File getOutputMediaFile(int type){}
这也应该包括在内!
答案 2 :(得分:0)
因此,您需要从相机拍摄的图像并在ImageView
上显示,不保存?
创建图片回调以从相机接收数据并将其解码为位图:
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, Camera c) {
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
yourImageView.setImageBitmap(bm);
}
};
调用相机捕获(单击捕获按钮时):
try {
mCamera.takePicture(null, null, mPicture);
} catch (Exception e) {
// take picture failed
e.printStackTrace();
}