I thought WINDOW_SERVICE was supposed to be a constant defined in android.content.Context.
http://developer.android.com/reference/android/content/Context.html#WINDOW_SERVICE
When I use it in the following segment of code it throws an error saying it cannot be resolved as a variable.
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
Here's the full code:
package com.commonsware.android.skeleton;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.*;
import android.os.Bundle;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import java.io.IOException;
import java.util.List;
// ----------------------------------------------------------------------
public class SimpleBulbActivity extends Activity {
private Preview mPreview;
FrameLayout preview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
protected void onResume() {
super.onResume();
//Setup the FrameLayout with the Camera Preview Screen
mPreview = new Preview(this);
preview = (FrameLayout)findViewById(R.id.preview);
preview.addView(mPreview);
}
}
// ----------------------------------------------------------------------
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
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) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, w, h);
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
parameters.setPreviewSize(h, w);
mCamera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
parameters.setPreviewSize(w, h);
}
if(display.getRotation() == Surface.ROTATION_180)
{
parameters.setPreviewSize(h, w);
}
if(display.getRotation() == Surface.ROTATION_270)
{
parameters.setPreviewSize(w, h);
mCamera.setDisplayOrientation(180);
}
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
Try...
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Related
I've been working with an app that has a camera activity that takes pictures. The normal behavior is to take a picture, send it as an intent and then close this activity, but on Samsung devices this just closes an restarts the app. I tested it on emulators and an Asus tablet and it worked fine. I also tested it on multiple OS versions but I haven't seen a difference. The Samsung devices with the problem are: S4, S2, and Note.
Here is the camera activity:
package app.xqute.com.xqute.ui;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import java.io.IOException;
import java.util.List;
import app.xqute.com.xqute.R;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import timber.log.Timber;
/**
* Created by mbarisa on 5/21/15.
*/
public class CameraActivity extends Activity {
#InjectView(R.id.cameraPreview)
FrameLayout mCameraPreview;
#InjectView(R.id.btnCapture)
ImageButton mBtnCapture;
private Camera mCamera;
private CameraPreview mPreview;
private int midScreenHeight, midScreenWidth;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
ButterKnife.inject(this);
mCamera = getCameraInstance();
// Create the Camera preview view
mPreview = new CameraPreview(this, mCamera);
mCameraPreview.addView(mPreview);
Display display = getWindowManager().getDefaultDisplay();
midScreenHeight = display.getHeight() / 2;
midScreenWidth = display.getWidth() / 2;
// TODO: remove this and enable faceDetectionListener
mBtnCapture.setVisibility(View.VISIBLE);
}
#OnClick(R.id.btnCapture)
void capture() {
// mCamera.setFaceDetectionListener(faceDetectionListener);
mCamera.takePicture(() -> {
}, null, mPicture);
}
final Camera.PictureCallback mPicture = (data, camera) -> {
System.out.println("data = " + data.length);
Intent intent = new Intent();
intent.putExtra("data", data);
setResult(RESULT_OK, intent);
finish();
};
final Camera.FaceDetectionListener faceDetectionListener = (faces, camera) -> {
try {
Timber.v("face: " + faces[0].rect.centerX() + " | " + faces[0].rect.centerY());
int posX = midScreenWidth - faces[0].rect.centerX();
int posY = midScreenHeight + faces[0].rect.centerY();
mBtnCapture.setVisibility(View.VISIBLE);
System.out.println("posY = " + posY);
System.out.println("posX = " + posX);
} catch (ArrayIndexOutOfBoundsException e) {
mBtnCapture.setVisibility(View.GONE);
}
};
#Override
protected void onPause() {
super.onPause();
finish();
mPreview.stopPreview();
}
/**
* A safe way to get an instance of the Camera object.
* <p>
* Returns null if camera is unavailable
*/
private static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
} catch (Exception ignore) {
// Camera is not available (in use or does not exist). Do nothing.
}
return c;
}
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private List<Camera.Size> mSupportedPreviewSizes;
private Camera.Size mPreviewSize;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview
try {
if (mCamera != null) {
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
// mCamera.setFaceDetectionListener(faceDetectionListener);
// mCamera.startFaceDetection();
}
} catch (IOException e) {
// Do nothing
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
}
}
private void stopPreview() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// Stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception ignore) {
// Tried to stop a non-existent preview. Do nothing.
}
try {
// Set preview size and make any resize, rotate or reformatting changes here
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
} catch (Exception ignored) {
}
// Start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception ignore) {
// Do nothing
}
}
#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);
}
}
}
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;
if (sizes == null) return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - h) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - h);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - h) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - h);
}
}
}
return optimalSize;
}
}
Any ideas why this is happening? Or maybe a way to fix or avoid this?
Im using a camera preview class which Ive copied from the API Demos.
I want to access the camera object mCamera in a function which I call from the oncreate() function in my MainActivity class.
But when I try to access it its null, because the SurfaceCreated() function in the PreviewClass wasn't called until then. I think I need to wait until the surfacecreated() gets called. But how can I call my function in the MainActivity from the PreviewClass? Is there a way to call my function in the MainActivity after the camera object gets initialized in the PreviewClass??? MAybe theres a way to redirect the callback to the MainActivity???
Heres the class:
package com.grasserapps.easymeasure;
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple wrapper around a Camera and a SurfaceView that renders a centered preview of the Camera
* to the surface. We need to center the SurfaceView because not all devices have cameras that
* support preview sizes at the same aspect ratio as the device's display.
*/
class CameraPreview extends ViewGroup implements SurfaceHolder.Callback {
private final String TAG = "Preview";
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
Size mPreviewSize;
List<Size> mSupportedPreviewSizes;
Camera mCamera;
Context c;
public static final String MY_PREFS = "MyPrefsFile";
CameraPreview(Context context) {
super(context);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
c= context;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
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();
}
}
public void switchCamera(Camera camera) {
setCamera(camera);
try {
camera.setPreviewDisplay(mHolder);
} catch (IOException exception) {
Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
}
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
requestLayout();
camera.setParameters(parameters);
}
#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);
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);
child.layout((width - previewWidth) / 2, 0, (width + previewWidth) / 2, height);
} else {
final int scaledChildHeight = previewHeight * width / previewWidth;
child.layout(0, (height - scaledChildHeight) / 2,
width, (height + scaledChildHeight) / 2);
}
}
}
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) {
Log.e(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 Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.01;
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) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
requestLayout();
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
Ive solved it like this:
In my preview calss Im saving the context into a global var Context context. Then I use it like so:
((MainActivity)context).functionInMainActivity;
I've implemented a custom camera activity in my app. The camera works pretty well on my nexus 5. However on an Alcatel One Touch Star, when I stop the preview to show the snapshot, I get this preview
Furthermore, the byte array of the PictureCallback is correct. Infact, if I decode the array into a Bitmap, I can see it correctly.
Here is my implementation of the SurfaceView as camera preview.
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private int mPhotoRotation;
public CameraPreview(Context context) {
super(context);
}
#SuppressLint("InlinedApi")
#SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
Camera.Parameters parameters = camera.getParameters();
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes != null && focusModes.contains(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
else if (focusModes != null && focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
if (parameters.getSupportedFlashModes() != null && parameters.getSupportedFlashModes().contains(Parameters.FLASH_MODE_AUTO))
parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
camera.setParameters(parameters);
mHolder = getHolder();
mHolder.addCallback(this);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (mCamera != null) {
mCamera.stopPreview();
Parameters parameters = mCamera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
Camera.Size size = getOptimalPreviewSize(previewSizes, width, height);
parameters.setPreviewSize(size.width, size.height);
mCamera.setParameters(parameters);
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
mPhotoRotation = 0;
if (display.getRotation() == Surface.ROTATION_0) {
mPhotoRotation = 90;
} else if (display.getRotation() == Surface.ROTATION_270) {
mPhotoRotation = 180;
}
mCamera.setDisplayOrientation(mPhotoRotation);
mCamera.startPreview();
}
}
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.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);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
public int getPhotoRotation() {
return mPhotoRotation;
}
}
When the user take the photo, this code is executed
mCamera.takePicture(null, null, mPictureCallback);
and the callback just keep a backup of the byte array and stop the preview.
mPictureCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
mPhotoJpeg = data;
mCamera.stopPreview();
}
};
So what is the cause of this behaviour? Why only on an alcatel and not on the nexus 5?
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpeg", System.currentTimeMillis()));
I'm trying to save an image from the camera onto the phone. I'm able to take the picture, see the taken picture on the SurfaceView and everything, but when I go to my phone's filesystem, the picture is nowhere to be found. I'm thinking it's this specific line of code, however I will post my entire Activity just to be sure.
package com.commonsware.android.skeleton;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.*;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
// ----------------------------------------------------------------------
public class SimpleBulbActivity extends Activity {
private Preview mPreview;
private static final String TAG = "CameraDemo";
FrameLayout preview;
Camera mCamera;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
protected void onResume() {
super.onResume();
//Setup the FrameLayout with the Camera Preview Screen
mPreview = new Preview(this);
preview = (FrameLayout)findViewById(R.id.preview);
preview.addView(mPreview);
}
public void snap(View view) {
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
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) {
FileOutputStream outStream = null;
try {
// write to local sandbox file system
// outStream =
// CameraDemo.this.openFileOutput(String.format("%d.jpg",
// System.currentTimeMillis()), 0);
// Or write to sdcard
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpeg", System.currentTimeMillis()));
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
// ----------------------------------------------------------------------
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
mCamera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(Environment.getExternalStorageDirectory().toString());
outStream.write(data);
outStream.close();
Log.d(TAG, "onPreviewFrame - wrote bytes: "
+ data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Preview.this.invalidate();
}
});
} catch (IOException e) {
mCamera.release();
mCamera = null;
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
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) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, w, h);
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
parameters.setPreviewSize(optimalSize.height, optimalSize.width);
mCamera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
}
if(display.getRotation() == Surface.ROTATION_180)
{
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
}
if(display.getRotation() == Surface.ROTATION_270)
{
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
mCamera.setDisplayOrientation(0);
}
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
}
do you have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in your AndroidManifest.xml?
I am following very closely to the sample codes in the resources for TicTacToeLib as well as an example of how to display a bitmap in a book titled Android Application Development. I am doing all the same things they are doing, the only difference is I am using a SurfaceView instead of a view. Can anyone tell me what I am doing wrong? The end goal is to display the preview image of the camera manually as well as display an image over top of it.
Sorry I forgot to include what the problem was. The onDraw method is not being called ever. and I was under the impression it should be called by the invalidate method. Secondly the lockCanvas() method is returning a NULL canvas. Even after I fixed the code to not use the PUSH_BUFFER constant from SurfaceHolder (I have edited the code for this correction).
package com.test.combine;
import android.content.res.XmlResourceParser;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import java.io.IOException;
import android.util.Log;
import java.util.List;
public class CombineTestActivity extends Activity
{
private Preview mPreview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Create our Preview view and set it as the content of our activity.
mPreview = new Preview(this);
setContentView(mPreview);
}
}
// ----------------------------------------------------------------------
class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private Paint mPaint;
private Canvas canvas;
private Bitmap currentprev;
private Picture mPicture;
private Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback()
{
public void onPreviewFrame( byte[] data, Camera Cam ) {
Log.d("CombineTestActivity", "Preview registered");
Log.d("CombineTestActivity", "Data length = "
+ data.length );
// currentprev = BitmapFactory.decodeByteArray( data, 0,
// data.length );
currentprev = BitmapFactory.decodeResource( getResources(),
R.drawable.creature00 );
if( currentprev == null )
Log.d("CombineTestActivity", "currentprev is null" );
Log.d("CombineTestActivity", "Preview Finished" );
invalidate();
}
};
private OnTouchListener mCorkyListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent me) {
Log.d("CombineTestActivity", "touch registered" );
mCamera.takePicture( null, null, mPicCallback );
return false;
}
};
private Camera.PictureCallback mPicCallback = new Camera.PictureCallback() {
public void onPictureTaken( byte[] data, Camera mCamera ) {
Log.d("CombineTestActivity", "picture method run" );
}
};
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mPicture = new Picture();
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
canvas = mHolder.lockCanvas();
if(canvas==null)
Log.d( "CombineTestActivity", "canvas is null in constructor");
this.setOnTouchListener( mCorkyListener );
invalidate();
}
#Override
protected void onDraw( Canvas mCanvas ) {
if( mCanvas == null )
Log.e("CombineTestActivity", "canvas is null" );
if( currentprev == null ) {
Log.e("CombineTestActivity", "currentprev is null" );
return;
}
mCanvas.drawBitmap( currentprev, 0.0f, 0.0f, mPaint );
Log.d("CombineTestActivity", "exiting onDraw" );
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
mCamera = Camera.open();
mCamera.setPreviewCallback( mPrevCallback );
/*
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
*/
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
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) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
if( sizes.isEmpty() )
Log.d( "CameraTestActivity", "sizes null");
else
Log.d( "CameraTestActivity", "sizes not null");
Size optimalSize = getOptimalPreviewSize(sizes, w, h);
if( optimalSize == null )
Log.d( "CameraTestActivity", "optimalSize null");
else
Log.d( "CameraTestActivity", "optimalSize not null");
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
if( parameters == null )
Log.d( "CameraTestActivity", "parameters null" );
else
Log.d( "CameraTestActivity", "parameters not null" );
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
I apparently found a solution. although SurfaceView inherits from View, it does not call onDraw like View does. As for why lockCanvas wasn't working, I still do not know.