我需要将视图转换为位图以预览我的视图并将其保存为图像.我尝试使用以下代码,但它创建了一个空白图像.我无法理解我犯了什么错误.
View viewToBeConverted; Bitmap viewBitmap = Bitmap.createBitmap(viewToBeConverted.getWidth(), viewToBeConverted.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);
viewToBeConverted.draw(canvas);
savephoto(“f1”, viewBitmap);
//// public void savephoto(String filename,Bitmap bit)
{
File newFile = new File(Environment.getExternalStorageDirectory() + Picture_Card/"+ filename+ ".PNG");
try
{
newFile.createNewFile();
try
{
FileOutputStream pdfFile = new FileOutputStream(newFile); Bitmap bm = bit; ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG,100, baos); byte[] bytes = baos.toByteArray();
pdfFile.write(bytes);
pdfFile.close();
}
catch (FileNotFoundException e)
{ //
}
} catch (IOException e)
{ //
}
}
Run Code Online (Sandbox Code Playgroud) 当Android 4.0(冰淇淋三明治)发布时,新的视图被引入到sdk中.这个视图是TextureView.在文档中,它表示TextureView可用于显示OpenGL场景的内容.
当您查找如何执行此操作时,您将找到一个示例链接.
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/U5RXFGpAHPE
但是我想用TextureView替换GLSurfaceView,并保持我的其余代码相同,并且只是接受TextureView的优点.
我使用以下代码.图像已保存,但它是黑色的.
请看我的代码并告诉我我做错了什么.
我在菜单中使用此代码.
case R.id.id_menu_Save:
Bitmap bmp = SavePixels(0, 0, 800, 400, CCDirector.sharedDirector().gl);
File file = new File("/sdcard/test.jpg");
try
{
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(CompressFormat.JPEG, 100, fos);
Toast.makeText(getApplicationContext(), "Image Saved", 0).show();
Log.i("Menu Save Button", "Image saved as JPEG");
}
catch (Exception e)
{
e.printStackTrace();
}
break;
Run Code Online (Sandbox Code Playgroud)
这是我的保存图像功能.
public static Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
{
int b[]=new int[w*(y+h)];
int bt[]=new int[w*h];
IntBuffer ib=IntBuffer.wrap(b);
ib.position(0);
gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
for(int …Run Code Online (Sandbox Code Playgroud) 问: 有没有人知道如何使用Tango Java(Jacobi)API onFrameAvailable()回调获取Tango的彩色相机图像缓冲区?
背景:
我有一个增强现实应用程序,在Tango的背景中显示视频.我在此示例后使用Java API(Jacobi)成功创建了视频叠加示例.我的应用程序工作正常,视频在后台正确呈现.
作为应用程序的一部分,我想在用户按下按钮时存储视频后备缓冲区的副本.因此,我需要访问相机的RGB数据.
根据Jacobi发行说明,任何希望访问相机RGB数据的类都应该实现新的onFrameAvailable()方法OnTangoUpdateListener.我做了这个,但我没有看到任何实际获取像素的句柄或参数:
Java API
@Override
public void onFrameAvailable(int cameraId) {
//Log.w(TAG, "Frame available!");
if (cameraId == TangoCameraIntrinsics.TANGO_CAMERA_COLOR) {
tangoCameraPreview.onFrameAvailable();
}
}
Run Code Online (Sandbox Code Playgroud)
如图所示,onFrameAvailable只有一个参数,整数表示生成视图的摄像机的id.将其与C库回调进行对比,后者提供对图像缓冲区的访问:
C API
TangoErrorType TangoService_connectOnFrameAvailable(
TangoCameraId id, void* context,
void (*onFrameAvailable)(void* context, TangoCameraId id,
const TangoImageBuffer* buffer));
Run Code Online (Sandbox Code Playgroud)
我期待Java方法在C API调用中有类似于缓冲区对象的东西.
我试过的
我尝试扩展TangoCameraPreview课程并将图像保存在那里,但我只获得黑色背景.
public class CameraSurfaceView extends TangoCameraPreview {
private boolean takeSnapShot = false;
public void takeSnapShot() {
takeSnapShot …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码对GlSurfaceView上的位图赋予多重效果. 应用效果,图像上,使用效果
现在,我想保存位图.他们给出了保存位图的代码,但是,整个GlSurfaceView将被保存为位图图像.相反,我想只保存位图区域以保存为图像.
有一种方法可以获取像素并从中制作位图并制作图像.例如:
public Bitmap takeScreenshot(GL10 mGL) {
final int mWidth = mEffectView.getWidth();
final int mHeight = mEffectView.getHeight();
IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
IntBuffer ibt = IntBuffer.allocate(mWidth * mHeight);
mGL.glReadPixels(0, 0, mWidth, mHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
// Convert upside down mirror-reversed image to right-side up normal
// image.
for (int i = 0; i < mHeight; i++) {
for (int j = 0; j < mWidth; j++) {
ibt.put((mHeight - i - 1) * mWidth + j, ib.get(i …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我具有一些按钮和SurfaceView的视图。我必须截取该视图的屏幕截图,但代替SurfaceView的只是黑场。我尝试在stackoverflow上找到的解决方案,但它们对我不起作用。
这是我通常会截取屏幕截图的代码:
File folder = new File(Environment.getExternalStorageDirectory().getPath() + "/folder");
if (!folder.exists()) {
folder.mkdir();
}
String path = folder.getAbsolutePath() + "snapshot.png";
File file = new File(path);
file.createNewFile();
View view = this.findViewById(android.R.id.content).getRootView();
view.setDrawingCacheEnabled(true);
Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(drawingCache);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fos);
} finally {
bitmap.recycle();
view.setDrawingCacheEnabled(false);
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
也许有人可以帮忙。
我徘徊了七个小时来解决我的问题.我创建了一个项目,其中我使用GLSurfaceView,我已经设置了一个图像,然后我使用EffectFactory类实现了不同的效果.
但我的问题是,当我拍摄带有影响的图像的屏幕截图时,它总是显示为黑屏而不是图像.我知道这可能是因为SurFaceview.但是,如果有任何建议可能对我有帮助.
提前致谢.
这是我的代码.
public class EffectsFilterActivity extends Activity implements
GLSurfaceView.Renderer {
private GLSurfaceView mEffectView;
private int[] mTextures = new int[2];
private EffectContext mEffectContext;
private Effect mEffect;
private TextureRenderer mTexRenderer = new TextureRenderer();
private int mImageWidth;
private int mImageHeight;
private boolean mInitialized = false;
int mCurrentEffect;
public void setCurrentEffect(int effect) {
mCurrentEffect = effect;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public …Run Code Online (Sandbox Code Playgroud) android ×7
screenshot ×4
bitmap ×2
opengl-es ×2
google-tv ×1
surfaceview ×1
textures ×1
view ×1