来自surfaceView的Android图片捕获
我正在尝试捕获图像,该图像在surfaceview中预览,并且有一个按钮,通过该按钮拍摄的照片可以保存到存储卡上。预览和捕获工作正常,但无法存储在存储卡上。。 有创建的文件,但图片不存储一个接一个。。。 请帮我。。。 我的试用版在这里来自surfaceView的Android图片捕获,android,android-camera,local-storage,Android,Android Camera,Local Storage,我正在尝试捕获图像,该图像在surfaceview中预览,并且有一个按钮,通过该按钮拍摄的照片可以保存到存储卡上。预览和捕获工作正常,但无法存储在存储卡上。。 有创建的文件,但图片不存储一个接一个。。。 请帮我。。。 我的试用版在这里 public class MainActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count=0; Camera mCamera; p
public class MainActivity extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
Camera mCamera;
private CameraView cameraview;
RelativeLayout mainlayout;
ImageView capture;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cameraview = new CameraView(this, CameraInfo.CAMERA_FACING_BACK);
setContentView(R.layout.activity_main);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
mainlayout.addView(cameraview);
capture=(ImageView)findViewById(R.id.capture);
/////////////////////////////////////////////////////////
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
capture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
cameraview.mCamera.takePicture(shutterCallback, rawCallback,
jpegCallback);
Toast.makeText(getApplicationContext(), "Captured", 2000).show();
}
});
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
// Log.d(TAG, "onShutter'd");
}
};
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//Log.d(TAG, "onPictureTaken - raw");
}
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format(
"/sdcard/Demo%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
我的摄影课在这里
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
int mCameraFacingInfo;
Context m_context;
public CameraView(Context context, int camereface) {
super(context);
// TODO Auto-generated constructor stub
m_context = context;
mCameraFacingInfo = camereface;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if (mCamera != null) {
int rotation = ((Activity) m_context).getWindowManager()
.getDefaultDisplay().getRotation();
if (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) {
mCamera.setDisplayOrientation(90);
} else if (rotation == Surface.ROTATION_90) {
mCamera.setDisplayOrientation(0);
} else if (rotation == Surface.ROTATION_270) {
mCamera.setDisplayOrientation(180);
}
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(width, height);
// mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// ////////////////////////////////////////////////////
/*mCamera.setCameraViewDisplay(holder);
mCamera.setCameraViewCallback(new CameraViewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
//
CameraView.this.invalidate();
}
});
*/
// //////////////////////////////////////////
synchronized (this) {
int cameraFacingInfo = -1;
boolean errorFound = false;
boolean hasFeatCamera = m_context.getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA);
if (hasFeatCamera) {
try {
cameraFacingInfo = mCameraFacingInfo;
mCamera = Camera.open(cameraFacingInfo);
} catch (Exception e) {
mCamera = Camera.open(0);
}
} else if (CameraInfo.CAMERA_FACING_FRONT > -1) {
try {
cameraFacingInfo = CameraInfo.CAMERA_FACING_FRONT;
mCamera = Camera.open(cameraFacingInfo);
} catch (Exception e) {
errorFound = true;
}
if (errorFound == true) {
try {
mCamera = Camera.open(0);
cameraFacingInfo = 0;
} catch (Exception e) {
cameraFacingInfo = -1;
}
}
}
if (cameraFacingInfo < 0) {
Toast.makeText(m_context, "No camera found.", Toast.LENGTH_LONG)
.show();
}
if (mCamera != null) {
try {
mCamera.setPreviewDisplay(holder);
int rotation = ((Activity) m_context).getWindowManager()
.getDefaultDisplay().getRotation();
if (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) {
// Log.i(TAG, "0");
mCamera.setDisplayOrientation(90);
} else if (rotation == Surface.ROTATION_90) {
// Log.i(TAG, "90");
mCamera.setDisplayOrientation(0);
} else if (rotation == Surface.ROTATION_270) {
// Log.i(TAG, "270");
mCamera.setDisplayOrientation(180);
}
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void setCameraFacingInfo(int cameraFacingInfo) {
mCameraFacingInfo = cameraFacingInfo;
}
}
公共类CameraView扩展了SurfaceView实现了SurfaceHolder.Callback{
表面粗糙度;
麦克默拉照相机;
麦卡梅拉法奇诺国际酒店;
语境m_语境;
公共CameraView(上下文,int-camereface){
超级(上下文);
//TODO自动生成的构造函数存根
m_context=上下文;
mcamerafacinfo=camerface;
mHolder=getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE\u TYPE\u PUSH\u缓冲区);
}
@凌驾
公共无效表面更改(表面文件夹持有者,整型格式,整型宽度,
整数高度){
//TODO自动生成的方法存根
if(mCamera!=null){
int rotation=((活动)m_上下文).getWindowManager()
.getDefaultDisplay().getRotation();
如果(旋转==表面旋转_0
||旋转==表面。旋转(180){
mCamera.setDisplayOrientation(90);
}else if(旋转==曲面旋转_90){
mCamera.setDisplayOrientation(0);
}else if(旋转==曲面旋转_270){
mCamera.setDisplayOrientation(180);
}
Camera.Parameters=mCamera.getParameters();
参数。setPreviewSize(宽度、高度);
//mCamera.setParameters(参数);
mCamera.startPreview();
}
}
@TargetApi(构建版本代码姜饼)
@SuppressLint(“新API”)
@凌驾
已创建的公共空白表面(表面持有人){
//TODO自动生成的方法存根
// ////////////////////////////////////////////////////
/*mCamera.setCameraViewDisplay(支架);
setCameraViewCallback(新的CameraViewCallback(){
预览帧上的公共无效(字节[]数据,摄像机arg1){
//
CameraView.this.invalidate();
}
});
*/
// //////////////////////////////////////////
已同步(此){
int cameraFacingInfo=-1;
布尔errorFound=false;
布尔值hasFeatCamera=m_context.getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_摄像头);
如果(照相机){
试一试{
Camerafacinfo=Mcamerafacinfo;
mCamera=Camera.open(cameraFacingInfo);
}捕获(例外e){
mCamera=Camera.open(0);
}
}否则如果(CameraInfo.CAMERA\u朝向\u前方>-1){
试一试{
cameraFacingInfo=Cameranfo.CAMERA面向前方;
mCamera=Camera.open(cameraFacingInfo);
}捕获(例外e){
errorFound=true;
}
if(errorFound==true){
试一试{
mCamera=Camera.open(0);
cameraFacingInfo=0;
}捕获(例外e){
cameraFacingInfo=-1;
}
}
}
if(cameraFacingInfo<0){
Toast.makeText(m_上下文,“找不到摄像头”,Toast.LENGTH_LONG)
.show();
}
if(mCamera!=null){
试一试{
mCamera.setPreviewDisplay(支架);
int rotation=((活动)m_上下文).getWindowManager()
.getDefaultDisplay().getRotation();
如果(旋转==表面旋转_0
||旋转==表面。旋转(180){
//Log.i(标签“0”);
mCamera.setDisplayOrientation(90);
}else if(旋转==曲面旋转_90){
//日志i(标签“90”);
mCamera.setDisplayOrientation(0);
}else if(旋转==曲面旋转_270){
//日志i(标签“270”);
mCamera.setDisplayOrientation(180);
}
}捕获(IOException异常){
mCamera.release();
mCamera=null;
//TODO:在此处添加更多异常处理逻辑
}
}
}
}
@凌驾
公共空间表面覆盖(表面覆盖物持有人){
//TODO自动生成的方法存根
if(mCamera!=null){
mCamera.stopPreview();
mCamera.release();
mCamera=null;
}
}
公共无效设置cameraFacingInfo(int cameraFacingInfo){
mcamerafacinfo=Camerafacinfo;
}
}
尝试将JPEG回调类更改为:
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//Creating empty file in sdcard
File pictureFile = new File(String.format("/sdcard/Demo%d.jpg", System.currentTimeMillis()));
if (pictureFile == null) {
Log.e("IMAGE CAPTURE", "Error creating media file, check storage permissions: ");
return;
}
if (data != null) {
BitmapFactory.Options opts = new BitmapFactory.Options();
ActivityManager activityManager = (ActivityManager) MainActivity.this.getSystemService(Activity.ACTIVITY_SERVICE);
int memoryLimit = 100;
if (activityManager != null) {
memoryLimit = activityManager.getMemoryClass();
}
// Considering memory limitation of device we will resize image to prevent OutOfMemory
if (memoryLimit < 20) {
opts.inSampleSize = 6;
} else if (memoryLimit < 40) {
opts.inSampleSize = 4;
} else if (memoryLimit < 64) {
opts.inSampleSize = 2;
}
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
if (bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) {
fos.close();
}
bitmap.recycle();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
}
PictureCallback jpegCallback=新建PictureCallback(){
公共void onPictureTaken(字节[]数据,摄像头){
//在SD卡中创建空文件
File pictureFile=新文件(String.format(“/sdcard/Demo%d.jpg”,System.currentTimeMillis());
如果(pictureFile==null){
Log.e(“图像捕获”,“创建媒体文件时出错,请检查存储权限:”);
返回;
}
如果(数据!=null){
BitmapFactory.Options opts=新的BitmapFactory.Options();
ActivityManager ActivityManager=(ActivityManager)MainActivity.this.getSystemService(Activity.Activity_服务);
int memoryLimit=100;
如果(activityManager!=null){
memoryLimit=activityManager.getMemoryClass();
}
//