【问题标题】:Saving a photo in black and white保存黑白照片
【发布时间】:2012-07-25 14:57:38
【问题描述】:

是否可以将使用彩色模式的相机拍摄的照片保存为黑白照片,或者必须更改相机参数以使取景器在拍摄照片时看到黑白照片?这是我用来保存彩色图像的代码,我想将其保存为黑白以节省空间。

   PictureCallback jpegCallback = new PictureCallback() {
    @Override
        public void onPictureTaken(byte[] data, Camera camera) { 
        Log.e("Pic Taken","Callback");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;

        options.inDither = false; // Disable Dithering mode
        options.inPurgeable = true; // Tell to gc that whether it needs free
                                    // memory, the Bitmap can be cleared
        options.inInputShareable = true; // Which kind of reference will be
                                            // used to recover the Bitmap
                                            // data after being clear, when
                                            // it will be used in the future
        options.inTempStorage = new byte[32 * 1024];
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length, options);

        int orientation;
        // others devices
        if(bMap.getHeight() < bMap.getWidth()){
            orientation = 90;
        } else {
            orientation = 0;
        }

        Bitmap bMapRotate;
        if (orientation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);
            bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(),
                    bMap.getHeight(), matrix, true);
        } else
            bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
                    bMap.getHeight(), true);


        FileOutputStream out;
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
        try {
              Log.e("Saving","Pic");
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = "/" + System.currentTimeMillis() + ".jpg";

            out = new FileOutputStream(baseDir + fileName);

            bMapRotate.compress(Bitmap.CompressFormat.JPEG, 25, out);
            if (bMapRotate != null) {
                bMapRotate.recycle();
                bMapRotate = null;
            }


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
};

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以转换位图:

    public Bitmap convertToGrayScale(Bitmap original) {        
        Bitmap result = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        ColorMatrix matrix = new ColorMatrix();
        matrix.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(matrix);
        paint.setColorFilter(f);
        canvas.drawBitmap(original, 0, 0, paint);
        return result;
    }
    

    【讨论】:

      【解决方案2】:

      您可以将现有照片转换为黑白照片。之后:

      else
              bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
                      bMap.getHeight(), true);
      

      添加这个:

      Bitmap newBitmap = Bitmap.createBitmap(bMapRotate.getWidth(), bMapRotate.getHeight(), Bitmap.Config.RGB_565);
      Canvas canvas = new Canvas(newBitmap);
      Paint paint = new Paint();
      ColorMatrix colorMatrix = new ColorMatrix();
      colorMatrix.setSaturation(0);
      ColorMatrixColorFilter cmFilter = new ColorMatrixColorFilter(colorMatrix);
      paint.setColorFilter(cmFilter);
      canvas.drawBitmap(bMapRotate, 0, 0, paint);
      

      别忘了在 bMapRotate 上调用 .recycle()。

      【讨论】:

        【解决方案3】:

        它应该在Camera.parameters 中可用。

        取决于设备的相机是否支持它,您可以使用它。

        使用此代码检索它们:

        Camera.Parameters params = cam.getParameters();
        try {           
            for (String e : params.getSupportedColorEffects()) {
                Log.d(TAG, "Effect: " + e);
            }
        }
        catch (NullPointerException npe) {
            Log.d(TAG, "No color effects supported by this camera.");           
        }
        

        并阅读文档:

        http://developer.android.com/reference/android/hardware/Camera.Parameters.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-14
          • 1970-01-01
          • 2012-08-15
          • 2015-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-22
          相关资源
          最近更新 更多