lundi 6 avril 2015

Android Studio-Camera using FrameLayout "eglSurfaceAttrib not implemented"


Vote count:

0




I am currently learning Android. Working on Android Studio 1.10. I have been trying this particular code that i had found, which adds a camera to a frameLayout.


It has a camera running on the top half of the screen, followed by a capture button and then an image.(Couldn't post a picture of the layout as i am not able to post my question otherwise)


activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.30"
android:orientation="vertical" >

<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="301dp" />

<Button
android:id="@+id/button_capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="snapIt"
android:text="@string/Capture" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />

</LinearLayout>

</LinearLayout>


MainActivity.java



import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends Activity {

private Camera cameraObject;
private ShowCamera showCamera;
private ImageView pic;
private static final String JPEG_FILE_SUFFIX = ".jpg";
private static final String JPEG_FILE_PREFIX = "IMG_";
public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
}
catch (Exception e){
}
return object;
}

private PictureCallback capturedIt = new PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {

Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);

String iconsStoragePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File sdIconStorageDir = new File(iconsStoragePath);
String timeStamp=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp;

sdIconStorageDir.mkdirs();

try {
String filePath = sdIconStorageDir.toString() + "/"+imageFileName;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);

BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);


bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);

bos.flush();
bos.close();

} catch (FileNotFoundException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());

} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());

}
cameraObject.release();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pic = (ImageView)findViewById(R.id.imageView1);
cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
public void snapIt(View view){
cameraObject.takePicture(null, null, capturedIt);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}


ShowCamera.java



import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder holdMe;
private Camera theCamera;

public ShowCamera(Context context,Camera camera) {
super(context);
theCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e) {
}
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}

}


However when the app starts i also get the following errors in the logcat:


I/OpenGLRenderer﹕ Initialized EGL, version 1.4


D/OpenGLRenderer﹕ Enabling debug mode 0


W/EGL_emulation﹕ eglSurfaceAttrib not implemented


W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6930180, error=EGL_SUCCESS


W/art﹕ Suspending all threads took: 30.807ms W/art﹕ Suspending all threads took: 51.086ms


On clicking the 'capture' button:


I/art﹕ Background sticky concurrent mark sweep GC freed 5729(267KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 1774KB/1774KB, paused 14.154ms total 24.125ms


Later it says that its shutting down the VM followed by a FatalException as it could not execute the onClick() method after release.


I need help with this as I am getting similar errors in whatever code I try out which uses SurfaceView class.


Thank you



asked 23 secs ago







Android Studio-Camera using FrameLayout "eglSurfaceAttrib not implemented"

Aucun commentaire:

Enregistrer un commentaire