我正在使用下面的代码捕获图像它工作正常但问题是当使用默认相机应用程序捕获图像时它捕获图像2048 * 1546px图像和大小是aprox 780到800kb并且在捕获时使用下面的代码捕获图像640 * 480,大小为70-80kb。所以我想拍摄全尺寸的图像。
public class CameraActivity extends Activity {
private static final String TAG = "CamActivity";
private Preview preview;
private Camera camera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView));
preview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
((FrameLayout) findViewById(R.id.preview)).addView(preview);
preview.setKeepScreenOn(true);
((Button) findViewById(R.id.buttonClick)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
((Button) findViewById(R.id.buttonClick)).setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean arg0, Camera arg1) {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
return true;
}
});
}
@Override
protected void onResume() {
super.onResume();
// preview.camera = Camera.open();
camera = Camera.open();
camera.startPreview();
preview.setCamera(camera);
}
@Override
protected void onPause() {
if (camera != null) {
camera.stopPreview();
preview.setCamera(null);
camera.release();
camera = null;
}
super.onPause();
}
private void resetCam() {
camera.startPreview();
preview.setCamera(camera);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
// Log.d(TAG, "onShutter'd");
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// Log.d(TAG, "onPictureTaken - raw");
}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Calendar c = Calendar.getInstance();
String temp = "" + c.get(Calendar.YEAR) + "-"
+ StaticData.pad(c.get(Calendar.MONTH) + 1) + "-"
+ StaticData.pad(c.get(Calendar.DATE)) + "-"
+ StaticData.pad(c.get(Calendar.HOUR)) + "-"
+ StaticData.pad(c.get(Calendar.MINUTE)) + "-"
+ StaticData.pad(c.get(Calendar.SECOND));
File sd = Environment.getExternalStorageDirectory();
File dir = new File(sd.getAbsolutePath() + "/" + HomeScreenActivity.MyDir);
String backupDBPath = "Image" + "_" + temp + ".jpg";
FileOutputStream outStream = null;
try {
File backupDB = new File(dir, backupDBPath);
outStream = new FileOutputStream(backupDB);
outStream.write(data);
outStream.close();
AppLog.logString(TAG + "onPictureTaken - wrote bytes: " + data.length);
resetCam();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
AppLog.logString(TAG + "onPictureTaken - jpeg");
}
};
}
Preview.java
class Preview extends ViewGroup implements SurfaceHolder.Callback {
private final String TAG = "Prev: ";
private SurfaceView mSurfaceView;
private SurfaceHolder mHolder;
private Size mPreviewSize;
private List<Size> mSupportedPreviewSizes;
private Camera mCamera;
Preview(Context context, SurfaceView sv) {
super(context);
mSurfaceView = sv;
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void setCamera(Camera camera) {
mCamera = camera;
if (mCamera != null) {
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
requestLayout();
Camera.Parameters params = mCamera.getParameters();
List<String> focusModes = params.getSupportedFocusModes();
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.setParameters(params);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// We purposely disregard child measurements because act as a
// wrapper to a SurfaceView that centers the camera preview instead
// of stretching it.
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
AppLog.logString(TAG+"1width : "+width);
AppLog.logString(TAG+"1height: "+height);
if (mSupportedPreviewSizes != null) {
AppLog.logString(TAG+"!=width : "+width);
AppLog.logString(TAG+"!=height: "+height);
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed && getChildCount() > 0) {
final View child = getChildAt(0);
final int width = r - l;
final int height = b - t;
int previewWidth = width;
int previewHeight = height;
AppLog.logString(TAG+"2width : "+width);
AppLog.logString(TAG+"2height: "+height);
if (mPreviewSize != null) {
previewWidth = mPreviewSize.width;
previewHeight = mPreviewSize.height;
}
// Center the child SurfaceView within the parent.
if (width * previewHeight > height * previewWidth) {
final int scaledChildWidth = previewWidth * height / previewHeight;
child.layout((width - scaledChildWidth) / 2, 0, (width + scaledChildWidth) / 2,height);
} else {
final int scaledChildHeight = previewHeight * width / previewWidth;
child.layout(0, (height - scaledChildHeight) / 2, width,(height + scaledChildHeight) / 2);
}
AppLog.logString(TAG+"3width : "+previewWidth);
AppLog.logString(TAG+"3height: "+previewHeight);
}
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where to draw.
try {
if (mCamera != null) {
mCamera.setPreviewDisplay(holder);
}
} catch (IOException exception) {
AppLog.errorLog(TAG + "IOException caused by setPreviewDisplay()", exception);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
if (mCamera != null) {
mCamera.stopPreview();
}
}
private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) {
Camera.Size result = null;
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
}
else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea > resultArea) {
result = size;
}
}
}
}
return (result);
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
AppLog.logString(TAG + "width :" + mPreviewSize.width);
AppLog.logString(TAG + "height :" + mPreviewSize.height);
AppLog.logString(TAG + "w :" + w);
AppLog.logString(TAG + "h :" + h);
parameters.setPreviewSize(mPreviewSize.width,mPreviewSize.height);
requestLayout();
mCamera.setParameters(parameters);
mCamera.startPreview();
mCamera.setZoomChangeListener(new OnZoomChangeListener() {
@Override
public void onZoomChange(int zoomValue, boolean stopped, Camera camera) {
// TODO Auto-generated method stub
}
});
}
}
}