I am trying to implement camera features on my application. Since it's my first time doing so, I'm running a little issue with my camera preview.
My current layout looks like this:
Which is producing a preview like this:
However I want something like this:
And here is the code for my camera:
public class FragmentPhoto extends Fragment implements TextureView.SurfaceTextureListener {
private Camera camera;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_photo, container, false);
final TextureView textureView = (TextureView) view.findViewById(R.id.photo_view);
textureView.setSurfaceTextureListener(this);
return view;
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
camera = Camera.open();
camera.setDisplayOrientation(90);
camera.setPreviewTexture(surface);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
camera.stopPreview();
camera.release();
return true;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Ignored, the Camera does all the work for us
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Update your view here!
}
}
Is there any way to crop the camera? To not make the preview this chubby? What is the best aproach to do this?
Related
I'm developing an application where I read some barcode. In a first step I had a big SurfaceView where I can see well the camera preview, but now I would set the dimensions of Surfaceview like the dimensions of barcode but I have bad camera visualization (it is too small). Can someone help me to stretch camera preview? Thanks
Here manage detector and surfaceview:
public class LettoreBarcode extends Fragment {
View view;
SurfaceView surfaceView;
Handler handler;
private BarcodeDetector detector;
private CameraSource cameraSource;
private TextView code;
SparseArray<Barcode> items;
private static Button btnBack;
String barcode = "" ;
SparseArray<Articoli> groups = new SparseArray<Articoli>();
Context _context = null;
ProductsAdapter.ViewHolder _ViewHolder = null;
public LettoreBarcode(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_barcode_scanner, container, false);
surfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);
detector = new BarcodeDetector.Builder(getActivity()).setBarcodeFormats(Barcode.ALL_FORMATS).build();
final Dialog d = new Dialog(getActivity());
btnBack = (Button) view.findViewById(R.id.btnBack);
handler = new Handler();
if(!detector.isOperational()){
Toast.makeText(getActivity(), "Detector non attivabile", Toast.LENGTH_LONG).show();
}
cameraSource = new CameraSource.Builder(getActivity(), detector).setAutoFocusEnabled(true).build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
AttivaCamera();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
detector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
items = detections.getDetectedItems();
if (items.size() > 0){
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
if (items.valueAt(0) != null){
//do something
handler.postDelayed(new Runnable() {
#Override
public void run() {
DisattivaCamera();
}
},10); //1000
}else
{
d.setContentView(R.layout.dialog_barcode_assente);
d.setTitle("Scanner");
d.setCancelable(true);
d.show();
DisattivaCamera();
}
}
});
}
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getActivity().onBackPressed();
}
});
return view;
}
private void AttivaCamera()
{
try{
cameraSource.start(surfaceView.getHolder());
}catch(IOException e){
Toast.makeText(getActivity(), "Errore nell'avvio della fotocamera", Toast.LENGTH_LONG).show();
}
}
private void DisattivaCamera()
{
cameraSource.stop();
}
}
It is how I visualize camera with small surfaceview:
https://i.stack.imgur.com/TMunJ.png
I'm new in android development so I'm sorry if could be a lot of mistake in the code.
Sorry for my english also..
Thanks you guys!
In order to display only part of the camera input, i.e. to crop it on the screen, you need a surface view that has dimensions that fit the camera frame aspect ratio, and overlay it with some nontransparent views to leave only part of it visible. Don't put the SurfaeView inside scrolling layout:
So, instead of
<SurfaceView width: match_parent height: 400dp />
you need e.g. FrameLayout as explained here: Is it possible to crop camera preview?
This will not change the frame that arrives to the barcode detector. But this should not worry you; the detector will handle the uncropped image correctly.
I want to get a preview of the camera on screen (Android Studio), the problem is that I'm in a class which extends a Fragment. I would like to have it on a surface view (with a dimension of about 1/6 of the screen size), in such a way that the preview is always running independently on the surface view while I capture pictures.
The code which take picture is running ok, the only thing that I can't do is to create the independent preview on surface view.
Any idea on how to accomplish that? Given that I'm in a Fragment and I've already created the method onCreateView.
I've searched online but I've only found code for preview plus taking picture.
The idea of what I want to do is the following, but it doesn't work in Fragment unfortunately:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
#SuppressWarnings("deprecation")
Camera camera; // camera class variable
SurfaceView camView; // drawing camera preview using this variable
SurfaceHolder surfaceHolder; // variable to hold surface for surfaceView which means display
boolean camCondition = false; // conditional variable for camera preview checking and set to false
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getWindow() to get window and set it's pixel format which is UNKNOWN
getWindow().setFormat(PixelFormat.UNKNOWN);
// refering the id of surfaceView
camView = (SurfaceView) findViewById(R.id.camerapreview);
// getting access to the surface of surfaceView and return it to surfaceHolder
surfaceHolder = camView.getHolder();
// adding call back to this context means MainActivity
surfaceHolder.addCallback(this);
// to set surface type
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
// stop the camera
if(camCondition){
camera.stopPreview(); // stop preview using stopPreview() method
camCondition = false; // setting camera condition to false means stop
}
// condition to check whether your device have camera or not
if (camera != null){
try {
Camera.Parameters parameters = camera.getParameters();
parameters.setColorEffect(Camera.Parameters.EFFECT_SEPIA); //applying effect on camera
camera.setParameters(parameters); // setting camera parameters
camera.setPreviewDisplay(surfaceHolder); // setting preview of camera
camera.startPreview(); // starting camera preview
camCondition = true; // setting camera to true which means having camera
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open(); // opening camera
camera.setDisplayOrientation(90); // setting camera preview orientation
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview(); // stopping camera preview
camera.release(); // releasing camera
camera = null; // setting camera to null when left
camCondition = false; // setting camera condition to false also when exit from application
}
}
The xml code is the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.tensorflow.demo.AutoFitTextureView
android:id="#+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<org.tensorflow.demo.RecognitionScoreView
android:id="#+id/results"
android:layout_width="match_parent"
android:layout_height="198dp"
android:layout_alignParentTop="true" />
<org.tensorflow.demo.OverlayView
android:id="#+id/debug_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="21dp"
android:layout_marginStart="12dp"
android:text="Capture" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/button"
android:layout_marginEnd="35dp"
android:text="Preview" />
<SurfaceView
android:id="#+id/surfaceView2"
android:layout_width="wrap_content"
android:layout_height="67dp"
android:layout_alignBottom="#+id/button"
android:layout_alignParentStart="true"
android:layout_marginStart="134dp" />
The initial part of code I have in my extended Fragment is the following:
public class CameraConnectionFragment extends Fragment {
private static final Logger LOGGER = new Logger();
/**
* The camera preview size will be chosen to be the smallest frame by pixel size capable of
* containing a DESIRED_SIZE x DESIRED_SIZE square.
*/
private static final int MINIMUM_PREVIEW_SIZE = 320;
/**
* Conversion from screen rotation to JPEG orientation.
*/
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
private static final String FRAGMENT_DIALOG = "dialog";
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
/**
* {#link android.view.TextureView.SurfaceTextureListener} handles several lifecycle events on a
* {#link TextureView}.
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.camera_connection_fragment, container, false);
Button capture = (Button) view.findViewById(R.id.button);
Button preview = (Button) view.findViewById(R.id.button2);
preview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
captureSession.capture(previewRequest, captureCallback, backgroundHandler);
}
catch (final CameraAccessException e) {
LOGGER.e(e, "Exception!");
}
}
});
return view;
}
What do you think I can do to have a continuos flow of image preview in the surfaceview?
I am trying to blur a LinearLayout in Service class.But when I try to blur the layout it blurs only the contents inside the layout.I want layout to be completely blurred so that behind that everything will look blurrish.
here is my code:
public class Exa extends Service {
LinearLayout LLayout,inner;
WindowManager windowManager;
RenderScript mRS;
Button button;
ImageView imageView;
ScriptIntrinsicBlur script;
Allocation allocOriginalScreenshot,allocBlurred;
TextureView textureViewBlurred;
private static final String TAG="MainActivity";
#Override
public void onCreate(){
final LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LLayout=(RelativeLayout)inflater.inflate(R.layout.activity_main,null);
LLayout=(LinearLayout)inflater.inflate(R.layout.blur_layout,null);
inner=(LinearLayout)LLayout.findViewById(R.id.innerL);
button=(Button)inner.findViewById(R.id.blurButton);
imageView=(ImageView)inner.findViewById(R.id.imageView2);
example();
windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams windowp=new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT);
windowManager.addView(LLayout,windowp);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
blurView(inner);
}
});
}
////all these code helps to give blurring effect
private void example() {
mRS=RenderScript.create(this);
script=ScriptIntrinsicBlur.create(mRS, Element.RGBA_8888(mRS));
script.setRadius(5);
}
Bitmap getViewScreenshot(View v){
v.setDrawingCacheEnabled(true);
Bitmap b=Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return b;
}
void replaceView(View originalView,View newView){
originalView.setTag(newView);
newView.setLayoutParams(new FrameLayout.LayoutParams(originalView.getLayoutParams()));
ViewGroup parent=(ViewGroup)originalView.getParent();
int index=parent.indexOfChild(originalView);
parent.removeView(originalView);
parent.addView(newView,index);
}
void restoreView(View v){
Log.i("hii","unblurring view");
View otherView=(View)v.getTag();
if (otherView!=null&&otherView.getParent()!=null){
replaceView(otherView,v);
}
else if (v!=null&&v.getParent()!=null){
replaceView(v,otherView);
}
}
void blurView(View v){
Bitmap viewScreenshot=getViewScreenshot(v);
if(allocOriginalScreenshot!=null&&(allocOriginalScreenshot.getType().getX()!=viewScreenshot.getWidth()||allocOriginalScreenshot.getType().getY()!=viewScreenshot.getHeight())){
allocOriginalScreenshot.destroy();
allocBlurred.destroy();
textureViewBlurred=null;
allocOriginalScreenshot=null;
allocBlurred=null;
}
if(allocOriginalScreenshot==null){
allocOriginalScreenshot=Allocation.createFromBitmap(mRS,viewScreenshot);
allocBlurred=Allocation.createTyped(mRS,allocOriginalScreenshot.getType(),Allocation.USAGE_SCRIPT|Allocation.USAGE_IO_OUTPUT);
textureViewBlurred=new TextureView(this);
textureViewBlurred.setOpaque(false);
textureViewBlurred.setSurfaceTextureListener(surfaceTextureListener);
}
else {
allocOriginalScreenshot.copyFrom(viewScreenshot);
}
replaceView(v,textureViewBlurred);
}
void unblur(View v){
restoreView(v);
}
void executeBlur(){
Log.d(TAG,"Executing blur");
script.setInput(allocOriginalScreenshot);
script.forEach(allocBlurred);
allocBlurred.ioSend();
}
TextureView.SurfaceTextureListener surfaceTextureListener=new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
allocBlurred.setSurface(new Surface(surface));
executeBlur();
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
I am making an application where I have a camera inside of a viewPager. I am wondering what would best be suited to "pause" and "resume" the camera so it doesn't hog resources when it is pre-loaded. I have the feeling that stopPreview is better suited for this as it does not release the camera but keeps it however it doesn't display the camera which is the main reason it hogs resources.
Enter & exit application: startCamera() & releaseCamera()
Tab visible & not visible: startPreview() & stop Preview()
Would this be a good rule of thumb?
I had a similar situation. :
If I kept camera (in ViewPager) in on state, the swipe were clunky and OOM exceptions were frequent.
Two options came in my mind:
shift the entire instance in a different thread
OR
use stopPreview() and startPreview()
I went with the second one :
However, instead of doing this on Fragment lifecycle callbacks I gave a button on the fragment which toggled the preview. Reason being, if user is swiping very fast, you can still receive OOm exception since the preview calls will be queued, especially if there are very few fragments in the viewPager.
In essence Release camera onPause(), acquire camera in onResume() and give a groovy button in fragment which will toggle your Preview on the surface!
Hello Karl I had the same things need to implement in view pager. I have circular viewer in which one fragment has the camera fragment. I want to handle the camera preview in such a way so it should not consume the camera resource.
As you know android view pager default load two fragment in to the memory. We implemented the view pager change listener and call the fragment method to start and stop the preview. even also destroy the camera preview in on destroy method of fragment.
class ViewPagerChangeListener implements ViewPager.OnPageChangeListener {
int currentPosition = DEFAULT_FRAGMENT;
#Override
public void onPageScrollStateChanged(int state) {
TimberLogger.d(TAG, "onPageScrollStateChanged");
}
#Override
public void onPageScrolled(int index, float arg1, int arg2) {
TimberLogger.d(TAG, "onPageScrolled" + index);
}
#Override
public void onPageSelected(int position) {
mWatchPosition = position;
TimberLogger.d(TAG, "onPageSelected" + mWatchPosition);
int newPosition = 0;
if (position > 4) {
newPosition = position;
}
TimberLogger.d(TAG, "newPosition" + newPosition);
/**
* Listener knows the new position and can call the interface method
* on new Fragment with the help of PagerAdapter. We can here call
* onResumeFragment() for new fragment and onPauseFragment() on the
* current one.
*/
// new fragment onResume
loadedFragment(newPosition).onResumeFragment();
// current fragment onPuase called
loadedFragment(currentPosition).onPauseFragment();
currentPosition = newPosition;
TimberLogger.d(TAG, "currentPosition" + currentPosition);
}
}
See the two method onResumeFragment and onPuaseFragment this two are the custom function each view pager fragment implements. In view pager change event we call the pause of current fragment and onResume of the new fragment.
// new fragment onResume
loadedFragment(newPosition).onResumeFragment();
// current fragment onPuase called
loadedFragment(currentPosition).onPauseFragment();
You can write your camera start preview inside custom method onResumeFragment and stop preview in onPauseFragment and also make sure you should override the onDestory() method of your camera fragment for release the camera resources.
The best solution will be to startCamera() in onResume(), and release it in onPause(), so you can handle, that camera is not free in onResume().
In ViewPager you can startPreview(), when fragment with it is selected, and stopPreview() otherwise. Also u can startPreview() in onCreateView() and stopPreview() in onDestroyView() in fragment.
This takes care of most of the operations CameraPreview.java:
package com.example.fela;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.ErrorCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import java.util.List;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Camera camera;
private int cameraId;
private Activity activity;
private CameraPreviewActivityInterface activityInterface;
public CameraPreview(Activity activity, int cameraId) {
super(activity);
try {
activityInterface = (CameraPreviewActivityInterface) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement ExampleFragmentCallbackInterface ");
}
this.activity = activity;
this.cameraId = cameraId;
holder = getHolder();
holder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {}
/**
* custom camera tweaks and startPreview()
*/
public void refreshCamera() {
if (holder.getSurface() == null || camera == null) {
// preview surface does not exist, camera not opened created yet
return;
}
Log.i(null, "CameraPreview refreshCamera()");
// stop preview before making changes
try {
camera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
int degrees = 0;
// specifically for back facing camera
switch (rotation) {
case Surface.ROTATION_0:
degrees = 90;
break;
case Surface.ROTATION_90:
degrees = 0;
break;
case Surface.ROTATION_180:
degrees = 270;
break;
case Surface.ROTATION_270:
degrees = 180;
break;
}
camera.setDisplayOrientation(degrees);
setCamera(camera);
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (Exception e) {
// this error is fixed in the camera Error Callback (Error 100)
Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.i(null, "CameraPreview surfaceChanged()");
// if your preview can change or rotate, take care of those events here.
// make sure to stop the preview before resizing or reformatting it.
// do not start the camera if the tab isn't visible
if(activityInterface.getCurrentPage() == 1)
startCamera();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {}
public Camera getCameraInstance() {
Camera camera = Camera.open();
// parameters for camera
Parameters params = camera.getParameters();
params.set("jpeg-quality", 100);
params.set("iso", "auto");
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
params.setPictureFormat(PixelFormat.JPEG);
// set the image dimensions
List<Size> sizes = params.getSupportedPictureSizes();
int max = 0, width = 0, height = 0;
for(Size size : sizes) {
if(max < (size.width*size.height)) {
max = (size.width*size.height);
width = size.width;
height = size.height;
}
}
params.setPictureSize(width, height);
camera.setParameters(params);
// primarily used to fix Error 100
camera.setErrorCallback(new ErrorCallback() {
#Override
public void onError(int error, Camera camera) {
if(error == Camera.CAMERA_ERROR_SERVER_DIED) {
releaseCamera();
startCamera();
}
}
});
return camera;
}
/**
* intitialize a new camera
*/
protected void startCamera() {
if(getCamera() == null)
setCamera(getCameraInstance());
refreshCamera();
}
/**
* release camera so other applications can utilize the camera
*/
protected void releaseCamera() {
// if already null then the camera has already been released before
if (getCamera() != null) {
getCamera().release();
setCamera(null);
}
}
public Camera getCamera() {
return camera;
}
public void setCamera(Camera camera) {
this.camera = camera;
}
public void setCameraId(int cameraId) {
this.cameraId = cameraId;
}
/**
* get the current viewPager page
*/
public interface CameraPreviewActivityInterface {
public int getCurrentPage();
}
}
In my FragmentCamera.java file:
private CameraPreview cameraPreview;
// code...
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// code...
cameraPreview = new CameraPreview(getActivity(), cameraId);
previewLayout.addView(cameraPreview);
// code...
}
// code...
#Override
public void onPause() {
super.onPause();
cameraPreview.releaseCamera();
}
#Override
public void onResume() {
super.onResume();
cameraPreview.startCamera();
}
protected void fragmentVisible() {
onResume();
}
protected void fragmentNotVisible() {
onPause();
}
And the MainActivity.java file (implements CameraPreviewActivityInterface):
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
currentPage = position;
if (currentPage == 1) {
fragmentCamera.fragmentVisible();
} else {
fragmentCamera.fragmentNotVisible();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
#Override
public int getCurrentPage() {
return currentPage;
}
I've seen lots of examples using TextureView in a main Activity but I'm trying to put it into a Fragment.
I've created the simplest example possible and its onCreateView is being called, onActivityCreated as well but onSurfaceTextureAvailable isn't being called after passing back the TextureView.
What am I missing ?
Thanks
G
public class FullscreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
}
}
public class TextureViewFragment extends Fragment
implements TextureView.SurfaceTextureListener {
private TextureView mTextureView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTextureView = new TextureView(getActivity());
mTextureView.setSurfaceTextureListener(this);
mTextureView.setOpaque(false);
return mTextureView;
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// TODO Auto-generated method stub
}
}
activity_fullscreen.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".FullscreenActivity" >
<fragment class="com.example.test.TextureViewFragment"
android:id="#+id/graphTextureView"
android:layout_width="0px" android:layout_height="match_parent" />
</FrameLayout>
This is because the TextureView must have a nonzero size and be visible. Based on the comment, the layout_width was set to 0px. Change that to a nonzero value.
(Old question but posting answer for posterity)
You should add 'android:hardwareAccelerated=true' to your AndroidManifest.xml.
TextureView needs hardware acceleration.
Your TextureView should be in a layout xml file. Instead of using new to instantiate it you should get it using something like this:
View view = inflater.inflate(R.layout.layout_video, container, false);
mTextureView = (TextureView) view.findViewById(R.id.video_texture_view);
Then in your layout_video.xml file you need a TextureView with id video_texture_view