/** Inform this View of the dimensions of frames coming from |stream|. */
 public void setSize(Endpoint stream, int width, int height) {
   // Generate 3 texture ids for Y/U/V and place them into |textures|,
   // allocating enough storage for |width|x|height| pixels.
   int[] textures = yuvTextures[stream == Endpoint.LOCAL ? 0 : 1];
   GLES20.glGenTextures(3, textures, 0);
   for (int i = 0; i < 3; ++i) {
     int w = i == 0 ? width : width / 2;
     int h = i == 0 ? height : height / 2;
     GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[i]);
     GLES20.glTexImage2D(
         GLES20.GL_TEXTURE_2D,
         0,
         GLES20.GL_LUMINANCE,
         w,
         h,
         0,
         GLES20.GL_LUMINANCE,
         GLES20.GL_UNSIGNED_BYTE,
         null);
     GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
     GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
     GLES20.glTexParameterf(
         GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
     GLES20.glTexParameterf(
         GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
   }
   checkNoGLES2Error();
 }
  static void initTexture(int id) {
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, id);

    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(
        GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); // Set U Wrapping
    GLES20.glTexParameterf(
        GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); // Set V Wrapping
  }
示例#3
0
  private void loadTexture(Bitmap b) {
    GLES20.glGenTextures(1, textures, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, b, 0);

    b.recycle();
  }
示例#4
0
  private static int createTexture() {
    int[] texture = new int[1];

    GLES20.glGenTextures(1, texture, 0);
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture[0]);
    GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);

    return texture[0];
  }
  /** Sets up texturing for the object */
  private void setupTextures(Object3D ob) {
    // create new texture ids if object has them
    if (ob.hasTexture()) {
      // number of textures
      int[] texIDs = ob.get_texID();
      int[] textures = new int[texIDs.length];
      _texIDs = new int[texIDs.length];
      // texture file ids
      int[] texFiles = ob.getTexFile();

      Log.d("TEXFILES LENGTH: ", texFiles.length + "");
      GLES20.glGenTextures(texIDs.length, textures, 0);

      for (int i = 0; i < texIDs.length; i++) {
        texIDs[i] = textures[i];

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIDs[i]);

        // parameters
        GLES20.glTexParameterf(
            GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameterf(
            GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

        InputStream is = mContext.getResources().openRawResource(texFiles[i]);
        Bitmap bitmap;
        try {
          bitmap = BitmapFactory.decodeStream(is);
        } finally {
          try {
            is.close();
          } catch (IOException e) {
            // Ignore.
          }
        }

        // create it
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();

        Log.d("ATTACHING TEXTURES: ", "Attached " + i);
      }
    }
  }
  public static int createTextureId(final int target) {
    final int[] textures = new int[1];

    // Generate one texture pointer...
    GLES20.glGenTextures(1, textures, 0);
    // ...and bind it to our array
    GLES20.glBindTexture(target, textures[0]);

    // Create Nearest Filtered Texture
    GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    // Different possible texture parameters, e.g. GLES20.GL_CLAMP_TO_EDGE
    GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
    return textures[0];
  }
  // 初始化纹理
  public int initTexture(int drawableId) {
    // 生成纹理ID
    int[] textures = new int[1];
    GLES20.glGenTextures(
        1, // 产生的纹理id的数量
        textures, // 纹理id的数组
        0 // 偏移量
        );
    int textureId = textures[0];
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
    // 非Mipmap纹理采样过滤参数
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    // ST方向纹理拉伸方式
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

    // 通过输入流加载图片===============begin===================
    InputStream is = this.getResources().openRawResource(drawableId);
    Bitmap bitmapTmp;
    try {
      bitmapTmp = BitmapFactory.decodeStream(is);
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    // 实际加载纹理,换成这个方法后,如果图片格式有问题,会抛出图片格式异常,不再会误显示其他异常
    GLUtils.texImage2D(
        GLES20.GL_TEXTURE_2D, // 纹理类型
        0,
        GLUtils.getInternalFormat(bitmapTmp),
        bitmapTmp, // 纹理图像
        GLUtils.getType(bitmapTmp),
        0 // 纹理边框尺寸
        );
    // 自动生成Mipmap纹理
    GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    // 释放纹理图
    bitmapTmp.recycle();
    // 返回纹理ID
    return textureId;
  }
  // Function for initializing the renderer.
  private void initRendering() {
    mTeapot = new Teapot();
    mTextPlane = new TextPlane();

    mRenderer = Renderer.getInstance();

    GLES20.glClearColor(0.0f, 0.0f, 0.0f, Vuforia.requiresAlpha() ? 0.0f : 1.0f);

    for (Texture t : mTextures) {
      GLES20.glGenTextures(1, t.mTextureID, 0);
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, t.mTextureID[0]);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexImage2D(
          GLES20.GL_TEXTURE_2D,
          0,
          GLES20.GL_RGBA,
          t.mWidth,
          t.mHeight,
          0,
          GLES20.GL_RGBA,
          GLES20.GL_UNSIGNED_BYTE,
          t.mData);
    }

    shaderProgramID =
        SampleUtils.createProgramFromShaderSrc(
            CubeShaders.CUBE_MESH_VERTEX_SHADER, CubeShaders.CUBE_MESH_FRAGMENT_SHADER);

    vertexHandle = GLES20.glGetAttribLocation(shaderProgramID, "vertexPosition");
    normalHandle = GLES20.glGetAttribLocation(shaderProgramID, "vertexNormal");
    textureCoordHandle = GLES20.glGetAttribLocation(shaderProgramID, "vertexTexCoord");
    mvpMatrixHandle = GLES20.glGetUniformLocation(shaderProgramID, "modelViewProjectionMatrix");
    texSampler2DHandle = GLES20.glGetUniformLocation(shaderProgramID, "texSampler2D");

    try {
      mBuildingsModel = new SampleApplication3DModel();
      mBuildingsModel.loadModel(mActivity.getResources().getAssets(), "ImageTargets/Buildings.txt");
    } catch (IOException e) {
      Log.e(LOGTAG, "Unable to load buildings");
    }

    // Hide the Loading Dialog
    mActivity.loadingDialogHandler.sendEmptyMessage(LoadingDialogHandler.HIDE_LOADING_DIALOG);
  }
示例#9
0
  public static int LoadTexture(GLSurfaceView view, int imgResID) {
    Log.d("Utils", "Loadtexture");
    Bitmap img = null;
    int textures[] = new int[1];
    try {
      img = BitmapFactory.decodeResource(view.getResources(), imgResID);
      GLES20.glGenTextures(1, textures, 0);

      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);

      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, img, 0);
      Log.d("LoadTexture", "Loaded texture" + ":H:" + img.getHeight() + ":W:" + img.getWidth());
    } catch (Exception e) {
      Log.d("LoadTexture", e.toString() + ":" + e.getMessage() + ":" + e.getLocalizedMessage());
    }
    img.recycle();
    return textures[0];
  }
示例#10
0
  public int initTexture(int drawableId) // textureId
      {
    // 生成纹理ID
    int[] textures = new int[1];
    GLES20.glGenTextures(
        1, // 产生的纹理id的数量
        textures, // 纹理id的数组
        0 // 偏移量
        );
    int textureId = textures[0];
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

    // 通过输入流加载图片===============begin===================
    InputStream is = this.getResources().openRawResource(drawableId);
    Bitmap bitmapTmp;
    try {
      bitmapTmp = BitmapFactory.decodeStream(is);
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    // 通过输入流加载图片===============end=====================

    // 实际加载纹理
    GLUtils.texImage2D(
        GLES20.GL_TEXTURE_2D, // 纹理类型,在OpenGL ES中必须为GL10.GL_TEXTURE_2D
        0, // 纹理的层次,0表示基本图像层,可以理解为直接贴图
        bitmapTmp, // 纹理图像
        0 // 纹理边框尺寸
        );
    bitmapTmp.recycle(); // 纹理加载成功后释放图片

    return textureId;
  }
示例#11
0
  public static int LoadTexture(GLSurfaceView view, String name) {
    Log.d("Utils", "Loadtexture");
    int textures[] = new int[1];
    Bitmap img = GetFromAssets(view, name);
    try {
      GLES20.glGenTextures(1, textures, 0);

      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, img, 0);
      Log.d("LoadTexture", "Loaded texture" + ":H:" + img.getHeight() + ":W:" + img.getWidth());
    } catch (Exception e) {
      Log.d("LoadTexture", e.toString() + ":" + e.getMessage() + ":" + e.getLocalizedMessage());
    }
    img.recycle();
    return textures[0];
  }
示例#12
0
 /** Initializes GL state. Call this after the EGL surface has been created and made current. */
 public void surfaceCreated() {
   mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
   if (mProgram == 0) {
     throw new RuntimeException("failed creating program");
   }
   maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
   checkGlError("glGetAttribLocation aPosition");
   if (maPositionHandle == -1) {
     throw new RuntimeException("Could not get attrib location for aPosition");
   }
   maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
   checkGlError("glGetAttribLocation aTextureCoord");
   if (maTextureHandle == -1) {
     throw new RuntimeException("Could not get attrib location for aTextureCoord");
   }
   muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
   checkGlError("glGetUniformLocation uMVPMatrix");
   if (muMVPMatrixHandle == -1) {
     throw new RuntimeException("Could not get attrib location for uMVPMatrix");
   }
   muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
   checkGlError("glGetUniformLocation uSTMatrix");
   if (muSTMatrixHandle == -1) {
     throw new RuntimeException("Could not get attrib location for uSTMatrix");
   }
   int[] textures = new int[1];
   GLES20.glGenTextures(1, textures, 0);
   mTextureID = textures[0];
   GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
   checkGlError("glBindTexture mTextureID");
   GLES20.glTexParameterf(
       GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
   GLES20.glTexParameterf(
       GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
   GLES20.glTexParameteri(
       GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
   GLES20.glTexParameteri(
       GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
   checkGlError("glTexParameter");
 }
  @SuppressWarnings("deprecation")
  public static int createTextureFromImage(final Context context, final int resId) {
    // Create an empty, mutable bitmap
    final Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
    // get a canvas to paint over the bitmap
    final Canvas canvas = new Canvas(bitmap);
    canvas.drawARGB(0, 0, 255, 0);

    // get a background image from resources
    // note the image format must match the bitmap format
    final Drawable background = context.getResources().getDrawable(resId);
    background.setBounds(0, 0, 256, 256);
    background.draw(canvas); // draw the background to our bitmap

    final int[] textures = new int[1];

    // Generate one texture pointer...
    GLES20.glGenTextures(1, textures, 0);
    // ...and bind it to our array
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    // Create Nearest Filtered Texture
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    // Different possible texture parameters, e.g. GLES20.GL_CLAMP_TO_EDGE
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

    // Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    // Clean up
    bitmap.recycle();

    return textures[0];
  }
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      mProgram = createProgram(mVertexShader, mFragmentShader);
      if (mProgram == 0) {
        return;
      }
      maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
      checkGlError("glGetAttribLocation aPosition");
      if (maPositionHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aPosition");
      }
      maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
      checkGlError("glGetAttribLocation aTextureCoord");
      if (maTextureHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aTextureCoord");
      }

      muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
      checkGlError("glGetUniformLocation uMVPMatrix");
      if (muMVPMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uMVPMatrix");
      }

      muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
      checkGlError("glGetUniformLocation uSTMatrix");
      if (muSTMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uSTMatrix");
      }

      int[] textures = new int[1];
      GLES20.glGenTextures(1, textures, 0);

      mTextureID = textures[0];
      GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID);
      checkGlError("glBindTexture mTextureID");

      GLES20.glTexParameterf(
          GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
      GLES20.glTexParameterf(
          GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

      mSurface = new SurfaceTexture(mTextureID);
      mSurface.setOnFrameAvailableListener(this);

      Surface surface = new Surface(mSurface);

      mMediaPlayer = new MediaPlayer();

      if (file != null) {
        try {
          mMediaPlayer.setDataSource(file.getAbsolutePath());
        } catch (IllegalArgumentException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (SecurityException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IllegalStateException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      } else if (filePath != null) {
        try {
          mMediaPlayer.setDataSource(filePath);
        } catch (IllegalArgumentException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (SecurityException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IllegalStateException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      } else if (uri != null) {
        try {
          mMediaPlayer.setDataSource(getContext(), uri);
        } catch (IllegalArgumentException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (SecurityException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IllegalStateException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      mMediaPlayer.setSurface(surface);
      surface.release();

      try {
        mMediaPlayer.prepare();
      } catch (IOException t) {
        Log.e(TAG, "media player prepare failed");
      }

      synchronized (this) {
        updateSurface = false;
      }

      mMediaPlayer.start();
    }
  // Returns true on success, false otherwise.
  @CalledByNative
  public boolean allocate(int width, int height, int frameRate) {
    Log.d(TAG, "allocate: requested (" + width + "x" + height + ")@" + frameRate + "fps");
    try {
      mCamera = Camera.open(mId);
    } catch (RuntimeException ex) {
      Log.e(TAG, "allocate: Camera.open: " + ex);
      return false;
    }

    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    Camera.getCameraInfo(mId, cameraInfo);
    mCameraOrientation = cameraInfo.orientation;
    mCameraFacing = cameraInfo.facing;
    mDeviceOrientation = getDeviceOrientation();
    Log.d(
        TAG,
        "allocate: orientation dev="
            + mDeviceOrientation
            + ", cam="
            + mCameraOrientation
            + ", facing="
            + mCameraFacing);

    Camera.Parameters parameters = mCamera.getParameters();

    // getSupportedPreviewFpsRange() returns a List with at least one
    // element, but when camera is in bad state, it can return null pointer.
    List<int[]> listFpsRange = parameters.getSupportedPreviewFpsRange();
    if (listFpsRange == null || listFpsRange.size() == 0) {
      Log.e(TAG, "allocate: no fps range found");
      return false;
    }
    int frameRateInMs = frameRate * 1000;
    // Use the first range as default.
    int[] fpsMinMax = listFpsRange.get(0);
    int newFrameRate = (fpsMinMax[0] + 999) / 1000;
    for (int[] fpsRange : listFpsRange) {
      if (fpsRange[0] <= frameRateInMs && frameRateInMs <= fpsRange[1]) {
        fpsMinMax = fpsRange;
        newFrameRate = frameRate;
        break;
      }
    }
    frameRate = newFrameRate;
    Log.d(TAG, "allocate: fps set to " + frameRate);

    // Calculate size.
    List<Camera.Size> listCameraSize = parameters.getSupportedPreviewSizes();
    int minDiff = Integer.MAX_VALUE;
    int matchedWidth = width;
    int matchedHeight = height;
    for (Camera.Size size : listCameraSize) {
      int diff = Math.abs(size.width - width) + Math.abs(size.height - height);
      Log.d(TAG, "allocate: supported (" + size.width + ", " + size.height + "), diff=" + diff);
      // TODO(wjia): Remove this hack (forcing width to be multiple
      // of 32) by supporting stride in video frame buffer.
      // Right now, VideoCaptureController requires compact YV12
      // (i.e., with no padding).
      if (diff < minDiff && (size.width % 32 == 0)) {
        minDiff = diff;
        matchedWidth = size.width;
        matchedHeight = size.height;
      }
    }
    if (minDiff == Integer.MAX_VALUE) {
      Log.e(TAG, "allocate: can not find a multiple-of-32 resolution");
      return false;
    }

    mCaptureFormat =
        new CaptureFormat(matchedWidth, matchedHeight, frameRate, BuggyDeviceHack.getImageFormat());
    // Hack to avoid certain capture resolutions under a minimum one,
    // see http://crbug.com/305294
    BuggyDeviceHack.applyMinDimensions(mCaptureFormat);
    Log.d(TAG, "allocate: matched (" + mCaptureFormat.mWidth + "x" + mCaptureFormat.mHeight + ")");

    if (parameters.isVideoStabilizationSupported()) {
      Log.d(
          TAG,
          "Image stabilization supported, currently: "
              + parameters.getVideoStabilization()
              + ", setting it.");
      parameters.setVideoStabilization(true);
    } else {
      Log.d(TAG, "Image stabilization not supported.");
    }
    parameters.setPreviewSize(mCaptureFormat.mWidth, mCaptureFormat.mHeight);
    parameters.setPreviewFormat(mCaptureFormat.mPixelFormat);
    parameters.setPreviewFpsRange(fpsMinMax[0], fpsMinMax[1]);
    mCamera.setParameters(parameters);

    // Set SurfaceTexture. Android Capture needs a SurfaceTexture even if
    // it is not going to be used.
    mGlTextures = new int[1];
    // Generate one texture pointer and bind it as an external texture.
    GLES20.glGenTextures(1, mGlTextures, 0);
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mGlTextures[0]);
    // No mip-mapping with camera source.
    GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    // Clamp to edge is only option.
    GLES20.glTexParameteri(
        GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(
        GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

    mSurfaceTexture = new SurfaceTexture(mGlTextures[0]);
    mSurfaceTexture.setOnFrameAvailableListener(null);

    try {
      mCamera.setPreviewTexture(mSurfaceTexture);
    } catch (IOException ex) {
      Log.e(TAG, "allocate: " + ex);
      return false;
    }

    int bufSize =
        mCaptureFormat.mWidth
            * mCaptureFormat.mHeight
            * ImageFormat.getBitsPerPixel(mCaptureFormat.mPixelFormat)
            / 8;
    for (int i = 0; i < NUM_CAPTURE_BUFFERS; i++) {
      byte[] buffer = new byte[bufSize];
      mCamera.addCallbackBuffer(buffer);
    }
    mExpectedFrameSize = bufSize;

    return true;
  }
示例#16
0
  /**
   * 设置适配器
   *
   * @param minFilter当纹理大小大于图元大小时使用的适配器
   * @param magFilter当纹理大小小于图元大小时使用的适配器
   */
  public void setFilter(float minFilter, float magFilter) {

    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, minFilter);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, magFilter);
  }
示例#17
0
 @Override
 public void glTexParameterf(int target, int pname, float param) {
   GLES20.glTexParameterf(target, pname, param);
 }
示例#18
0
  public void loadGLTexture(Context context, int picture) {

    // loading texture

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), picture);

    // generate one texture pointer
    GLES20.glGenTextures(8, textures, 0);

    // ...and bind it to our array
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    // create nearest filtered texture
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    // Use Android GLUtils to specify a two-dimensional texture image from our bitmap

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    bitmap = BitmapFactory.decodeResource(context.getResources(), picture);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[1]);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    bitmap = BitmapFactory.decodeResource(context.getResources(), picture);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[2]);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    bitmap = BitmapFactory.decodeResource(context.getResources(), picture);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[3]);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    bitmap = BitmapFactory.decodeResource(context.getResources(), picture);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[4]);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    bitmap = BitmapFactory.decodeResource(context.getResources(), picture);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[5]);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    bitmap = BitmapFactory.decodeResource(context.getResources(), picture);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[6]);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    bitmap = BitmapFactory.decodeResource(context.getResources(), picture);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[7]);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();
  }