| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.test.hwui; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.graphics.Bitmap; |
| 21 |
import android.graphics.SurfaceTexture; |
| 22 |
import android.hardware.Camera; |
| 23 |
import android.os.Bundle; |
| 24 |
import android.os.Environment; |
| 25 |
import android.view.Gravity; |
| 26 |
import android.view.TextureView; |
| 27 |
import android.view.View; |
| 28 |
import android.widget.Button; |
| 29 |
import android.widget.FrameLayout; |
| 30 |
|
| 31 |
import java.io.FileNotFoundException; |
| 32 |
import java.io.FileOutputStream; |
| 33 |
import java.io.IOException; |
| 34 |
|
| 35 |
@SuppressWarnings({"UnusedDeclaration"}) |
| 36 |
public class GetBitmapActivity extends Activity implements TextureView.SurfaceTextureListener { |
| 37 |
private Camera mCamera; |
| 38 |
private TextureView mTextureView; |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void onCreate(Bundle savedInstanceState) { |
| 42 |
super.onCreate(savedInstanceState); |
| 43 |
|
| 44 |
FrameLayout content = new FrameLayout(this); |
| 45 |
|
| 46 |
mTextureView = new TextureView(this); |
| 47 |
mTextureView.setSurfaceTextureListener(this); |
| 48 |
|
| 49 |
Button button = new Button(this); |
| 50 |
button.setText("Copy bitmap to /sdcard/textureview.png"); |
| 51 |
button.setOnClickListener(new View.OnClickListener() { |
| 52 |
@Override |
| 53 |
public void onClick(View v) { |
| 54 |
Bitmap b = mTextureView.getBitmap(); |
| 55 |
try { |
| 56 |
FileOutputStream out = new FileOutputStream( |
| 57 |
Environment.getExternalStorageDirectory() + "/textureview.png"); |
| 58 |
try { |
| 59 |
b.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 60 |
} finally { |
| 61 |
try { |
| 62 |
out.close(); |
| 63 |
} catch (IOException e) { |
| 64 |
|
| 65 |
} |
| 66 |
} |
| 67 |
} catch (FileNotFoundException e) { |
| 68 |
|
| 69 |
} |
| 70 |
} |
| 71 |
}); |
| 72 |
|
| 73 |
content.addView(mTextureView, new FrameLayout.LayoutParams(500, 400, Gravity.CENTER)); |
| 74 |
content.addView(button, new FrameLayout.LayoutParams( |
| 75 |
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, |
| 76 |
Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM)); |
| 77 |
setContentView(content); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { |
| 82 |
mCamera = Camera.open(); |
| 83 |
|
| 84 |
try { |
| 85 |
mCamera.setPreviewTexture(surface); |
| 86 |
} catch (IOException t) { |
| 87 |
android.util.Log.e("TextureView", "Cannot set preview texture target!", t); |
| 88 |
} |
| 89 |
|
| 90 |
mCamera.startPreview(); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { |
| 100 |
mCamera.stopPreview(); |
| 101 |
mCamera.release(); |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void onSurfaceTextureUpdated(SurfaceTexture surface) { |
| 107 |
|
| 108 |
} |
| 109 |
} |