/** * Capture the current image with the size as it is displayed and retrieve it as Bitmap. * * @return current output as Bitmap * @throws InterruptedException */ public Bitmap capture() throws InterruptedException { final Semaphore waiter = new Semaphore(0); final int width = mGLSurfaceView.getMeasuredWidth(); final int height = mGLSurfaceView.getMeasuredHeight(); // Take picture on OpenGL thread final int[] pixelMirroredArray = new int[width * height]; mGPUImage.runOnGLThread( new Runnable() { @Override public void run() { final IntBuffer pixelBuffer = IntBuffer.allocate(width * height); GLES20.glReadPixels( 0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuffer); int[] pixelArray = pixelBuffer.array(); // Convert upside down mirror-reversed image to right-side up normal image. for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { pixelMirroredArray[(height - i - 1) * width + j] = pixelArray[i * width + j]; } } waiter.release(); } }); requestRender(); waiter.acquire(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.copyPixelsFromBuffer(IntBuffer.wrap(pixelMirroredArray)); return bitmap; }
private void init(Context context, AttributeSet attrs) { mGLSurfaceView = new GPUImageGLSurfaceView(context, attrs); mGLSurfaceView.setZOrderOnTop(true); mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT); addView(mGLSurfaceView); mGPUImage = new GPUImage(getContext()); mGPUImage.setGLSurfaceView(mGLSurfaceView); }
@Override public void run() { // from File try { File file = new File(getFolderNmae() + File.separator + String.valueOf(model.key.hashCode())); if (file.exists()) { Bitmap bitmap = BitmapFactory.decodeFile(file.getPath()); model.bitmap = bitmap; return; } GPUImage gpuImage = new GPUImage(mContext); gpuImage.setImage(model.bitmap); gpuImage.setFilter(model.mGPUImageFilter); Bitmap bitmap = gpuImage.getBitmapWithFilterApplied(); model.bitmap = bitmap; if (SDCardUtil.isSDCardAvaiable()) { saveBitmap( mContext, bitmap, mSaveType, getFolderNmae(), String.valueOf(model.key.hashCode()), false); } return; } catch (Exception e) { e.printStackTrace(); } finally { imageCache.put(model.key, new SoftReference<Bitmap>(model.bitmap)); Message msg = Message.obtain(); msg.what = HANDLER_SEND_CODE; msg.obj = model; mHandler.sendMessage(msg); } }
// ギャラリー、カメラから戻ってきたときの処理をする関数 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { Bitmap bm = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; // 元の1/4サイズでbitmap取得 switch (requestCode) { case REQUEST_CAMERA: System.out.println(bitmapUri.getPath()); bm = BitmapFactory.decodeFile(bitmapUri.getPath(), options); // 撮影した画像をギャラリーのインデックスに追加されるようにスキャンする。 // これをやらないと、アプリ起動中に撮った写真が反映されない String[] paths = {bitmapUri.getPath()}; String[] mimeTypes = {"image/*"}; MediaScannerConnection.scanFile( getApplicationContext(), paths, mimeTypes, new MediaScannerConnection.OnScanCompletedListener() { @Override public void onScanCompleted(String path, Uri uri) {} }); break; case REQUEST_GALLERY: try { if (data != null || data.getData() != null) { bitmapUri = data.getData(); InputStream is = getContentResolver().openInputStream(data.getData()); bm = BitmapFactory.decodeStream(is, null, options); is.close(); } } catch (OutOfMemoryError e) { e.printStackTrace(); bm = null; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } break; default: finish(); break; } ExifInterface exif = null; try { exif = new ExifInterface(bitmapUri.getPath()); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } Matrix matrix = new Matrix(); if (exif != null) { int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); if (orientation == 6) { matrix.postRotate(90); } else if (orientation == 3) { matrix.postRotate(180); } else if (orientation == 8) { matrix.postRotate(270); } } bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); gpuImage.setImage(bm); imageView.setImageBitmap(bm); } else { finish(); } }
// 画像にフィルタをかける関数 public void changeFilter(float val) { // 今何のフィルタが選択されているか分岐 switch (nowFilter) { // Sepia のケース (場合) case Sepia: // gpuImage にフィルタと値をセット gpuImage.setFilter(new GPUImageSepiaFilter(val)); break; // Gray のケース (場合) case Gray: gpuImage.setFilter(new GPUImageGrayscaleFilter()); break; case Sharp: gpuImage.setFilter(new GPUImageSharpenFilter(val)); break; case Edge: gpuImage.setFilter(new GPUImageSobelEdgeDetection()); break; case Gamma: gpuImage.setFilter(new GPUImageGammaFilter(val)); break; case Brightness: gpuImage.setFilter(new GPUImageBrightnessFilter(val)); break; case BulgeDistortion: gpuImage.setFilter(new GPUImageBulgeDistortionFilter()); break; case Shapen: gpuImage.setFilter(new GPUImageSharpenFilter(val)); break; case Emboss: gpuImage.setFilter(new GPUImageEmbossFilter(val)); break; case GaussianBlur: gpuImage.setFilter(new GPUImageGaussianBlurFilter(val)); break; case HighLightShadow: gpuImage.setFilter(new GPUImageHighlightShadowFilter(val, (float) 0.5)); break; case Saturation: gpuImage.setFilter(new GPUImageSaturationFilter(val)); break; case Exposure: gpuImage.setFilter(new GPUImageExposureFilter(val)); break; case Contrast: gpuImage.setFilter(new GPUImageContrastFilter(val)); break; case Opacity: gpuImage.setFilter(new GPUImageOpacityFilter(val)); break; case Threshold: gpuImage.setFilter(new GPUImageSobelThresholdFilter(val)); break; default: break; } try { // gpuImageView に フィルタがかかった画像をセット imageView.setImageBitmap(gpuImage.getBitmapWithFilterApplied()); } catch (NullPointerException e) { e.printStackTrace(); } filterType.setText("TYPE: " + nowFilter.toString()); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); // 入れ物に中身入れる if (gpuImage == null) gpuImage = new GPUImage(this); imageView = (ImageView) findViewById(R.id.gpuimage); filterType = (TextView) findViewById(R.id.filter_type); final SeekBar seekBar = (SeekBar) findViewById(R.id.seek); final TextView textView = (TextView) findViewById(R.id.value); // シークバーが操作されたときの処理 seekBar.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // シークバーの最大値は 100 // 取得したい値は 0 ~ 1 の値なので /100 する // 関数に後の処理はお任せ float val = (float) progress / 100; textView.setText("VALUE: " + String.valueOf(val)); changeFilter(val); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); // アニメーションをセット inAnim = AnimationUtils.loadAnimation(this, R.anim.in_anim); outAnim = AnimationUtils.loadAnimation(this, R.anim.out_anim); // 画像を押したときに消すレイアウトグループを呼ぶ final RelativeLayout relativeLayout1 = (RelativeLayout) findViewById(R.id.menu_top); final RelativeLayout relativeLayout2 = (RelativeLayout) findViewById(R.id.menu_bottom); // 画像を押したときの処理 imageView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { animationView(relativeLayout1); animationView(relativeLayout2); } }); twitter = TwitterUtils.getTwitterInstance(this); try { gpuImage.getBitmapWithFilterApplied(); } catch (NullPointerException e) { getPicture(); } setButtonOnClickEvent(); }
@Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); mGPUImage.deleteImage(); mGPUImage.setImage(bitmap); }
/** * Retrieve current image with filter applied and given size as Bitmap. * * @param width requested Bitmap width * @param height requested Bitmap height * @return Bitmap of picture with given size * @throws InterruptedException */ public Bitmap capture(final int width, final int height) throws InterruptedException { // This method needs to run on a background thread because it will take a longer time if (Looper.myLooper() == Looper.getMainLooper()) { throw new IllegalStateException("Do not call this method from the UI thread!"); } mForceSize = new Size(width, height); final Semaphore waiter = new Semaphore(0); // Layout with new size getViewTreeObserver() .addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { getViewTreeObserver().removeOnGlobalLayoutListener(this); } waiter.release(); } }); post( new Runnable() { @Override public void run() { // Show loading addView(new LoadingView(getContext())); mGLSurfaceView.requestLayout(); } }); waiter.acquire(); // Run one render pass mGPUImage.runOnGLThread( new Runnable() { @Override public void run() { waiter.release(); } }); requestRender(); waiter.acquire(); Bitmap bitmap = capture(); mForceSize = null; post( new Runnable() { @Override public void run() { mGLSurfaceView.requestLayout(); } }); requestRender(); postDelayed( new Runnable() { @Override public void run() { // Remove loading view removeViewAt(1); } }, 300); return bitmap; }
/** * Sets the image on which the filter should be applied from a File. * * @param file the file of the new image */ public void setImage(final File file) { mGPUImage.setImage(file); }
/** * Sets the image on which the filter should be applied from a Uri. * * @param uri the uri of the new image */ public void setImage(final Uri uri) { mGPUImage.setImage(uri); }
/** * Sets the image on which the filter should be applied. * * @param bitmap the new image */ public void setImage(final Bitmap bitmap) { mGPUImage.setImage(bitmap); }
/** * Set the filter to be applied on the image. * * @param filter Filter that should be applied on the image. */ public void setFilter(GPUImageFilter filter) { mFilter = filter; mGPUImage.setFilter(filter); requestRender(); }
/** * Sets the rotation of the displayed image. * * @param rotation new rotation */ public void setRotation(Rotation rotation) { mGPUImage.setRotation(rotation); requestRender(); }
/** * Set the scale type of GPUImage. * * @param scaleType the new ScaleType */ public void setScaleType(GPUImage.ScaleType scaleType) { mGPUImage.setScaleType(scaleType); }
// TODO Should be an xml attribute. But then GPUImage can not be distributed as .jar anymore. public void setRatio(float ratio) { mRatio = ratio; mGLSurfaceView.requestLayout(); mGPUImage.deleteImage(); }