Hi,
I'm developping an application where the user can take a picture and then
send it to the server. But unfortunally some pictures cannot be taken from
the nexus s.
I try the same picture with HTC Tatto and Samsung Galaxy S and there are no
probleme with these devices. It's generally picture from outside.
Here is my code:
private Preview mPreview;
private Camera camera;
private byte[] pictureData;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
mPreview = new Preview(this);
mPreview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
takePicture();
}
});
setContentView(mPreview);
LayoutInflater controlInflater =
LayoutInflater.from(getBaseContext());
View viewControl =
controlInflater.inflate(R.layout.activity_edit_action_camera, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
}
@Override
protected void onResume() {
super.onResume();
camera = Camera.open();
mPreview.setCamera(camera);
}
@Override
protected void onPause() {
super.onPause();
if (camera != null) {
mPreview.setCamera(null);
camera.release();
camera = null;
}
}
AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
@Override
public void onAutoFocus(boolean success, Camera camera) {
}
};
private void takePicture(){
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
class Preview extends ViewGroup implements SurfaceHolder.Callback {
private final String TAG = "Preview";
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
Size mPreviewSize;
List<Size> mSupportedPreviewSizes;
Camera mCamera;
Preview(Context context) {
super(context);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
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();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = resolveSize(getSuggestedMinimumWidth(),
widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(),
heightMeasureSpec);
setMeasuredDimension(width, height);
if (mSupportedPreviewSizes != null) {
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;
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);
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
try {
if (mCamera != null) {
mCamera.setPreviewDisplay(holder);
Camera.Parameters parameters = mCamera.getParameters();
List<String> flashModes =
parameters.getSupportedFlashModes();
if (flashModes!=null &&flashModes.size() > 0)
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
// Action mode take pictures of fast moving objects
List<String> sceneModes =
parameters.getSupportedSceneModes();
if(sceneModes!=null && sceneModes.size()>0){
if
(sceneModes.contains(Camera.Parameters.SCENE_MODE_ACTION))
parameters.setSceneMode(Camera.Parameters.SCENE_MODE_ACTION);
else
parameters.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
}
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.autoFocus(myAutoFocusCallback);
mCamera.setParameters(parameters);
}
} catch (IOException exception) {
Log.e(TAG, "IOException caused by setPreviewDisplay()",
exception);
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
}
}
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) {
Camera.Parameters parameters = mCamera.getParameters();
if (mSupportedPreviewSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, w,
h);
}
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
parameters.setPictureSize(mPreviewSize.width, mPreviewSize.height);
List<String> flashModes = parameters.getSupportedFlashModes();
if (flashModes!=null && flashModes.size() > 0)
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
requestLayout();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.autoFocus(myAutoFocusCallback);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
Is there anything in my code that's wrong or anything I forgot?
I already try to delete the sceneMode, the flashMode and the autoFocus but
it still have the problem without them.
Thanks,
Sebastien
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en