I am developing an Android Application. I created a custom camera class to capture images. It is working fine in som many devices but when i Tried it with Samsung Galaxy S4 it returns image with gray lins as shown. My Code is :
![`*public void surfaceCreated(SurfaceHolder holder) {
frontCam = SharedUserPrefs.getSharedPrefData(AutoCapture.this,
Constants.IS_FRONT_CAMERA);
if (frontCam.equals(Constants.VALUE_ON)) {
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
mCamera = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(TAG,
"Camera failed to open: "
+ e.getLocalizedMessage());
}
}
}
} else {
mCamera = Camera.open();
}
mCamera.setDisplayOrientation(90);
Camera.Parameters p = mCamera.getParameters();
if (getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
if (flashOn.equals(Constants.VALUE_ON)) {
{
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
}
} else {
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
p.setPictureFormat(ImageFormat.JPEG);
List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();
Camera.Size previewsize = previewSizes.get(0);
for (Camera.Size size : previewSizes) {
Log.d("Width---" + size.width, "Height---" + size.height);
}
p.setPreviewSize(previewsize.width, previewsize.height);
mCamera.setParameters(p);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.e(TAG, "surfaceChanged");
if (mPreviewRunning) {
mCamera.stopPreview();
}
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
System.out.println("Caught exception in surface chagned");
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "surfaceDestroyed");
if (mPreviewRunning == false) {
if (mCamera != null) {
mPreviewRunning = false;
mCamera.stopPreview();
mCamera.release();
}
if (mCamera != null) {
mPreviewRunning = false;
mCamera.release();
}
}/*
* else if (mCamera != null) { mPreviewRunning = false;
* mCamera.stopPreview(); mCamera.release(); }
*/
}*`][2]
I think you have the same issue me and this other guy did.
Samsung Galaxy S4 , Image gets corrupted while taking from camera
Related
I have the some of the following code to use the camera:
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCamera == null) {
mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void resetCamera() {
if (!videoReset) {
if (videoStarted && !videoStopped) {
mMediaRecorder.stop();
}
MediaScannerConnection.scanFile(TherapistActivity.this, new String[]{videoFile.getAbsolutePath()}, null, null);
mMediaRecorder.reset();
mMediaRecorder.release();
mCamera.release();
mCamera = null;
mMediaRecorder = null;
videoReset = true;
initSuccess = false;
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
surfaceWidth = width;
surfaceHeight = height;
try {
if (!initSuccess)
startPreview(mHolder.getSurface());
} catch (IOException e) {
e.printStackTrace();
}
}
private void initRecorder(Surface surface) throws IOException {
Camera.Parameters parameters = mCamera.getParameters();
Camera.Size previewSize = getOptimalPreviewSize(surfaceWidth, surfaceHeight);
if (previewSize != null) {
parameters.setPreviewSize(previewSize.width, previewSize.height);
}
parameters.setVideoStabilization(false);
mCamera.setParameters(parameters);
mCamera.startPreview();
mCamera.unlock();
if (mMediaRecorder == null) mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoEncodingBitRate(3072 * 1000);
mMediaRecorder.setVideoFrameRate(60);
mMediaRecorder.setVideoSize(1280, 720);
mMediaRecorder.setOutputFile(videoFile.getAbsolutePath());
if (!mApp.videoTime) {
mMediaRecorder.setMaxDuration(30000);
} else {
mMediaRecorder.setMaxDuration(Integer.MAX_VALUE);
}
mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
mCameraWrapper.setBackgroundColor(Color.TRANSPARENT);
try {
mCamera.stopPreview();
videoRecorded = true;
} catch (RuntimeException re) {
Log.e("Error", "Could not stop camera!");
} finally {
videoPreviewStarted = false;
}
btRecord.setTag("stop");
btRecord.setBackgroundResource(R.drawable.stop_nobkgrnd_gray);
tvVideoCountdown.setVisibility(View.GONE);
initSuccess = false;
}
}
});
try {
mMediaRecorder.prepare();
videoPreviewStarted = true;
} catch (IllegalStateException e) {
e.printStackTrace();
}
initSuccess = true;
}
}
private Camera.Size getOptimalPreviewSize(int w, int h) {
Camera.Size result = null;
Camera.Parameters p = mCamera.getParameters();
for (Camera.Size size : p.getSupportedPreviewSizes()) {
if (size.width <= w && size.height <= h) {
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;
}
On the Astro Tab A10 running Marshmallow, apparently the Camera isn't being released properly when being run the second time, since the camera fails to open until the device is rebooted. I am pretty sure this isn't a permissions problem, since I granted the app permissions to the camera at runtime as well as including camera permissions in the manifest.
Does anyone know what might be the problem?
EDIT: Here is the stack trace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mobilityresearch.treadmill3, PID: 4144
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:495)
at android.hardware.Camera.open(Camera.java:341)
at com.mobilityresearch.treadmill3.TherapistActivity.surfaceCreated(TherapistActivity.java:8260)
at android.view.SurfaceView.updateWindow(SurfaceView.java:583)
at android.view.SurfaceView.setVisibility(SurfaceView.java:257)
at com.mobilityresearch.treadmill3.TherapistActivity$26.onClick(TherapistActivity.java:1701)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:742)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
This doesn't happen on any of our other tablets. In fact, it doesn't happen on the older version of the Astro Tab A10 running Android 5.1 Lollipop. I tried calling resetCamera the camera both in surfaceCreated before opening the preview, and surfacedDestroyed after closing the preview, and it doesn't work.
EDIT 2: I just found out that if I actually record video, it works fine, but if I don't record video and only display the preview, it doesn't work.
EDIT 3: Added surfaceChanged and initRecorder. Updated resetCamera.
You are missing call to stopPreview() which stops capturing and drawing preview frames to the surface and setPreviewDisplay(null) which releases the preview display.
Update your resetCamera() as follows:
private void resetCamera() {
...
mCamera.stopPreview();
mCamera.setPreviewDisplay(null);
mCamera.release();
...
}
}
My program is suppose to store a byte[] image from the camera in an ArrayList every 250 ms.
I am testing the same code on 2 separate devices:
the first is a Nexus 7 running Android 4.4.2 and the second is a Nexus 10 running Android 4.4.3.
The Nexus 7 runs the scheduleWithFixedDelay function perfectly, but the Nexus 10 does not. The Nexus 10 runs it once and then stops.
class RunnableCamera implements Runnable {
ScheduledExecutorService serviceCam = Executors.newSingleThreadScheduledExecutor();
#Override
public void run() {
startCamera CAM = new startCamera();
serviceCam.scheduleWithFixedDelay(new Runnable()
{
#Override
public void run()
{
camera.takePicture(null, null, mPicture);
}
}, 0, 250, TimeUnit.MILLISECONDS);
}
private PictureCallback mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
camera.startPreview();
imagesArrayList.add(data);
Log.d("image", "byte[] added to ArrayList");
}
};
public class startCamera implements Callback {
//private Camera camera;
private SurfaceHolder holder = null;
public startCamera() {
videoView = (VideoView) findViewById(R.id.vwCamera);
holder = videoView.getHolder();
holder.addCallback(this);
Log.d("bro", "startCamera() constructor");
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
Log.i("bro", "surfaceCreated");
camera = Camera.open(0);
camera.setDisplayOrientation(90);
if (camera != null) {
camera.setPreviewDisplay(holder);
} else {
Log.i("bro", "camera = null");
}
} catch (Exception e) {
Log.v(TAG, "Could not start the preview-Display123");
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.i("bro", "surfaceChanged");
//Sets correct preview resolution
if (holder.getSurface() == null){
Log.i("bro", "holder.getSurface() == null");
return;
}
Camera.Parameters parameters = camera.getParameters();
Log.i("bro", "camera.getParameters();");
sizes = parameters.getSupportedPreviewSizes();
Log.i("bro", "parameters.getSupportedPreviewSizes();");
Camera.Size optimalSize = getBestPreviewSize(width, height);
try {
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
camera.setParameters(parameters);
} catch (NullPointerException a) {
}
Log.i("bro", "startPreview()");
camera.startPreview();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("bro", "surfaceDestroyed");
if (camera != null) {
camera.stopPreview();
camera.release();
Log.i("bro", "CAMERA STOPPED");
}
}
private Camera.Size getBestPreviewSize(int width, int height) {
Camera.Size result = null;
Camera.Parameters p = camera.getParameters();
for (Camera.Size size : p.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;
}
}
}
In my onCreate method I start the Runnable class like:
Thread thread1 = new Thread(new RunnableCamera());
thread1.start();
If I change the time delay to 600 ms, the Nexus 10 runs it perfectly. I'm wondering if this is a device issue with the first generation Nexus 10, an Android 4.4.3 issue or something wrong with my code. Is there a better way to do this?
I'm implementing camera activity and have encountered orientation issue.
While I'm succeeding to define preview's orientation, the final movie is with wrong orientation by 90 degrees always.
I have tried all the suggestions, those I have found here and it's only defines the proper preview, not the recorded movie.
I'm using next method to start recording:
private void startRecording() {
mRecordingButton.setBackgroundResource(R.drawable.rec_stop);
mSwitchCameraButton.setVisibility(View.INVISIBLE);
mSendButton.setVisibility(View.INVISIBLE);
mChronometer.setVisibility(View.VISIBLE);
try {
mMediaRecorder = new MediaRecorder();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(0x00000003);
mMediaRecorder.setVideoEncoder(VideoEncoder.H264);
mMediaRecorder.setOutputFile(getFile().getPath());
mMediaRecorder.setVideoSize(480, 720);
mMediaRecorder.setVideoEncodingBitRate(BPS_VIDEO);
mMediaRecorder.setVideoFrameRate(24);
mMediaRecorder.setAudioChannels(2);
mMediaRecorder.setAudioEncodingBitRate(BPS_AUDIO);
// mMediaRecorder.setOrientationHint(90);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.prepare();
mMediaRecorder.start();
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.start();
mIsRecording = true;
} catch (Exception e) {
e.printStackTrace();
}
}
and this to define orientation:
public void surfaceCreated(SurfaceHolder arg0) {
mCamera = Camera.open(0);
Camera.Parameters params = mCamera.getParameters();
List<Size> sizes = params.getSupportedPictureSizes();
Size mSize = sizes.get(0);
params.setPictureSize(mSize.width, mSize.height);
if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
Display display =
((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_0) {
setDisplayOrientation(mCamera, 90);
} else if (display.getRotation() == Surface.ROTATION_270) {
setDisplayOrientation(mCamera, 180);
}
} else {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
params.set("orientation", "portrait");
params.set("rotation", 90);
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
params.set("orientation", "landscape");
params.set("rotation", 90);
}
}
mCamera.setParameters(params);
// setCameraDisplayOrientation(this, 0, mCamera);
previewCamera();
if (mCamera == null) {
Toast.makeText(
this.getApplicationContext(),
"Camera is not available!",
Toast.LENGTH_SHORT).show();
finish();
}
}
public void previewCamera() {
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d("VideoCameraActivity", "Cannot start preview", e);
}
}
P.S. This line solves the problem:
mMediaRecorder.setOrientationHint(90);
but causes to crash some of the devices (galaxy s1, s3).
I have a customer with an LG Optimus Black (android 2.2.2) that have the camera preview every time black. On other devices all is working fine. Is there some issue on this phone or someone have a solution? The code for my camera preview is the follow.
SurfaceHolder.Callback mySurfaceHolderCallback = new SurfaceHolder.Callback()
{
#Override
public void surfaceCreated(SurfaceHolder holder)
{
Log.i("GMG", "surfaceCreated");
try
{
mCamera = Camera.open();
holder.setFormat(PixelFormat.TRANSLUCENT);
mCamera.setPreviewDisplay(holder);
}
catch (IOException e)
{
if (mCamera == null) return;
mPreviewRunning= false;
mCamera.release();
mCamera = null;
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.i("GMG", "surfaceDestroyed");
if (mCamera != null)
{
mCamera.stopPreview();
mPreviewRunning= false;
mCamera.release();
mCamera = null;
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Log.i("GMG", "surfaceChanged");
if (mCamera != null)
{
if(mPreviewRunning) mCamera.stopPreview();
Camera.Parameters p = mCamera.getParameters();
//Angolo visivo della fotocamera
angoloVisualeX = p.getHorizontalViewAngle();
angoloVisualeY = p.getVerticalViewAngle();
//Formati della preview
supportedPreviewSize = p.getSupportedPreviewSizes();
int preview_width = supportedPreviewSize.get(supportedPreviewSize.size()-1).width;
int preview_height = supportedPreviewSize.get(supportedPreviewSize.size()-1).height;
p.setPreviewSize(preview_width, preview_height);
//Set camera orientation
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0) mCamera.setDisplayOrientation(90);
if(display.getRotation() == Surface.ROTATION_270) mCamera.setDisplayOrientation(180);
mCamera.setParameters(p);
mCamera.startPreview();
mPreviewRunning = true;
}
}
};
Best regards.
I have solved, I don't know why, but witha big camera preview I dont have the problem
change
int preview_width = supportedPreviewSize.get(supportedPreviewSize.size()-1).width;
int preview_height = supportedPreviewSize.get(supportedPreviewSize.size()-1).height;
with
int preview_width = supportedPreviewSize.get(0).width;
int preview_height = supportedPreviewSize.get(0).height;
I've run into such a problem. The problem is that when I use onPictureTaken function the light is automatically go down. What should I do with it? I want the light not to turn off.
Photo :
if (v == shot)
{
camera.takePicture (null, null, null, this);
}
flashlight:
if (prefs.getBoolean (getString (R.string.torch), false))
{
Parameters params = camera.getParameters ();
params.setFlashMode (Parameters.FLASH_MODE_TORCH);
camera.setParameters (params);
}
#Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
camera.setPreviewCallback(this);
camera.setPreviewDisplay(holder);
Camera.Parameters p = camera.getParameters();
p.setPictureSize(1024, 768);
p.setPictureFormat (PixelFormat.RGB_565);
camera.setParameters(p);
}
catch (IOException e)
{
e.printStackTrace();
}
LayoutParams lp = preview.getLayoutParams();
if (this.getResources().getConfiguration().orientation != Configuration.UI_MODE_TYPE_CAR)
{
camera.setDisplayOrientation(90);
}
else
{
camera.setDisplayOrientation(0);
}
preview.setLayoutParams(lp);
camera.startPreview();
return;
}
#Override
protected void onPause()
{
if (camera != null)
{
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
}
super.onPause();
return ;
}
protected void onResume()
{
super.onResume();
camera = Camera.open();
}