Skip to content
Snippets Groups Projects
Commit bf548aab authored by Maxime Graulich's avatar Maxime Graulich
Browse files

Android: share current model

parent e2cb7a8c
No related branches found
No related tags found
No related merge requests found
package org.geuz.onelab;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Handler;
public class GLESRender implements Renderer{
Gmsh mGModel;
Handler loadingHand;
private Gmsh mGModel;
private int _width, _height;
private Bitmap _screenshot;
private boolean _needScreenshot;
public GLESRender(Gmsh model) {
this.mGModel = model;
_needScreenshot = false;
}
public void load(String filename){
......@@ -44,13 +49,36 @@ public class GLESRender implements Renderer{
// OpenGL ES methods
public void onDrawFrame(GL10 gl) {
mGModel.viewDraw();
if(_needScreenshot) this.screenshot(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
mGModel.viewInit(width, height);
_width = width + 5;
_height = height;
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) { }
public void needScreenshot() {_screenshot = null; _needScreenshot = true;}
public Bitmap getScreenshot(){return _screenshot;}
private void screenshot(GL10 gl)
{
_needScreenshot = false;
int bitmapBuffer[] = new int[_width * _height];
int bitmapSource[] = new int[_width * _height];
IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
intBuffer.position(0);
gl.glReadPixels(0, 0, _width, _height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
int offset1, offset2;
for (int i = 0; i < _height; i++) {
offset1 = i * _width;
offset2 = (_height - i - 1) * _width;
for (int j = 0; j < _width; j++) {
int pixel = bitmapBuffer[offset1 + j];
bitmapSource[offset2 + j] = (pixel & 0xFF00FF00) | (pixel << 16) & 0x00FF0000 | (pixel >> 16) & 0x000000FF;
}
}
_screenshot = Bitmap.createBitmap(bitmapSource, _width, _height, Bitmap.Config.ARGB_8888);
}
}
package org.geuz.onelab;
import java.io.File;
import java.util.ArrayList;
import android.app.ActionBar;
......@@ -14,6 +15,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
......@@ -92,6 +94,8 @@ public class MainActivity extends Activity{
}
_runStopMenuItem = menu.add((_compute)?R.string.menu_stop:R.string.menu_run);
_runStopMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuItem shareMenuItem = menu.add("Share ...");
shareMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
return true;
}
@Override
......@@ -115,6 +119,16 @@ public class MainActivity extends Activity{
}
else if(item.getTitle().equals(getString(R.string.menu_stop))){
_gmsh.onelabCB("stop");
}
else if(item.getTitle().equals("Share ...")) {
File file = new File(this.getExternalFilesDir(null), "onelab_screenshot.png");
file.setReadable(true, false);
_modelFragment.takeScreenshot(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, "Share screenshot with ..."));
}
else if(item.getItemId() == android.R.id.home)
{
......
package org.geuz.onelab;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Gravity;
......@@ -78,4 +83,16 @@ public class ModelFragment extends Fragment{
public void requestRender() {
_glView.requestRender();
}
public void takeScreenshot(File out) {
Bitmap screenshot = _glView.getScreenshot();
try {
FileOutputStream f = new FileOutputStream(out);
screenshot.compress(Bitmap.CompressFormat.PNG, 85, f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
_glView.setDrawingCacheEnabled(false);
}
}
}
......@@ -20,6 +20,8 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
......@@ -42,7 +44,7 @@ public class ModelList extends Activity {
} catch (IOException e) {
e.printStackTrace();
}
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
ListView list = new ListView(this);
......@@ -113,6 +115,22 @@ public class ModelList extends Activity {
layout.setBackgroundColor(Color.argb(255, 67, 67, 67));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem loadFile = menu.add(R.string.button_open_external_file);
loadFile.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if(item.getTitle().equals(getString(R.string.button_open_external_file))) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(this.getFilesDir()));
startActivity(browserIntent);
}
return super.onMenuItemSelected(featureId, item);
}
private void getModels() throws XmlPullParserException, IOException
{
File document = this.getFilesDir();
......
......@@ -95,7 +95,7 @@ public class OptionsDisplayFragment extends Fragment{
if(_gmsh == null) return;
String[] PViews = _gmsh.getPView();
for(int i=0; i<_listView.itemsCountInSection("Result"); i++) {
View v = (View)_listView.getItemAtPosition(7+i);
View v = (View)_listView.getItemAtPosition(6+i);
if(!v.getClass().equals(LinearLayout.class)) continue;
for(int j=0; j<((LinearLayout)v).getChildCount(); j++) {
View sv = ((LinearLayout)v).getChildAt(j);
......
package org.geuz.onelab;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView;
import android.support.v4.view.MotionEventCompat;
import android.view.GestureDetector;
......@@ -124,5 +125,11 @@ class mGLSurfaceView extends GLSurfaceView {
public void resetScale(){
scaleFactor = 1f;
_renderer.scaleModel(scaleFactor);
}
}
public Bitmap getScreenshot() {
_renderer.needScreenshot();
this.requestRender();
while(_renderer.getScreenshot() == null);
return _renderer.getScreenshot();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment