2017-06-20 92 views
0

對所有Android開發人員您好!拍攝和保存時的Android自定義相機圖片圖片完全黑色

我正在使用自定義相機(Android NDK),我將在其中應用一些過濾器。現在確定,但是當我捕獲圖片並保存它時,圖片會變成完全黑色的圖像。首先,我轉換拍攝的圖像以位圖和解碼它,更多的瞭解我在這裏向您發送我的完整代碼....

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_camera); 

    toolbar = (Toolbar) findViewById(R.id.downloadToolbal); 
    setSupportActionBar(toolbar); 

    context = CameraActivity.this; 
    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      finish(); 
     } 
    }); 

    cameraEngine = new CameraEngine(this); 

    MagicEngine.Builder builder = new MagicEngine.Builder(); 
    magicEngine = builder.build((MagicCameraView) findViewById(R.id.gl_surface_camera)); 
    camera_image = (ImageView)findViewById(R.id.cameraImg); 
    cameraSurface = (RelativeLayout)findViewById(R.id.camera_surface); 
    cameraSurface.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      cameraEngine.takePicture(mPicture); 
     } 
    }); 

    mFilterListView = (RecyclerView) findViewById(R.id.filter_listView); 
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); 
    mFilterListView.setLayoutManager(linearLayoutManager); 
    mAdapter = new FilterAdapter(this, types); 
    mFilterListView.setAdapter(mAdapter); 
    mAdapter.setOnFilterChangeListener(new FilterAdapter.onFilterChangeListener() { 
     @Override 
     public void onFilterChanged(MagicFilterType filterType) { 
      magicEngine.setFilter(filterType); 
     } 
    }); 
} 

也看到這裏

private Camera.PictureCallback mPicture = new Camera.PictureCallback() { 

    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 
     dir_image2 = new File(Environment.getExternalStorageDirectory()+ 
       File.separator+"My Custom Folder"); 
     dir_image2.mkdirs(); 


     File tmpFile = new File(dir_image2,"TempImage.jpg"); 
     try { 
      fos = new FileOutputStream(tmpFile); 
      fos.write(data); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); 
     } 
     options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 

     bmp1 = decodeFile(tmpFile); 
     bmp=Bitmap.createScaledBitmap(bmp1,cameraSurface.getWidth(), cameraSurface.getHeight(),true); 
     camera_image.setImageBitmap(bmp); 
     tmpFile.delete(); 
     savePicture(); 

    } 
}; 


private static Bitmap decodeFile(File f) { 
    Bitmap b = null; 
    try { 
     // Decode image size 
     o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 

     fis = new FileInputStream(f); 
     BitmapFactory.decodeStream(fis, null, o); 
     fis.close(); 
     int IMAGE_MAX_SIZE = 1000; 
     int scale = 1; 
     if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { 
      scale = (int) Math.pow(
        2, 
        (int) Math.round(Math.log(IMAGE_MAX_SIZE 
          /(double) Math.max(o.outHeight, o.outWidth)) 
          /Math.log(0.5))); 
     } 

     // Decode with inSampleSize 
     o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     fis = new FileInputStream(f); 
     b = BitmapFactory.decodeStream(fis, null, o2); 
     fis.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return b; 
} 

如果你想知道MagicEngine那麼這裏的類是貝婁

public class MagicEngine { 
private static MagicEngine magicEngine; 

public static MagicEngine getInstance(){ 
    if(magicEngine == null) 
     throw new NullPointerException("MagicEngine must be built first"); 
    else 
     return magicEngine; 
} 

private MagicEngine(Builder builder){ 

} 

public void setFilter(MagicFilterType type){ 
    MagicParams.magicBaseView.setFilter(type); 
} 

public void savePicture(File file, SavePictureTask.OnPictureSaveListener listener){ 
    SavePictureTask savePictureTask = new SavePictureTask(file, listener); 
    MagicParams.magicBaseView.savePicture(savePictureTask); 
} 

public void startRecord(){ 
    if(MagicParams.magicBaseView instanceof MagicCameraView) 
     ((MagicCameraView)MagicParams.magicBaseView).changeRecordingState(true); 
} 

public void stopRecord(){ 
    if(MagicParams.magicBaseView instanceof MagicCameraView) 
     ((MagicCameraView)MagicParams.magicBaseView).changeRecordingState(false); 
} 

public void setBeautyLevel(int level){ 
    if(MagicParams.magicBaseView instanceof MagicCameraView && MagicParams.beautyLevel != level) { 
     MagicParams.beautyLevel = level; 
     ((MagicCameraView) MagicParams.magicBaseView).onBeautyLevelChanged(); 
    } 
} 

public void switchCamera(){ 
    CameraEngine.switchCamera(); 
} 

public static class Builder{ 

    public MagicEngine build(MagicBaseView magicBaseView) { 
     MagicParams.context = magicBaseView.getContext(); 
     MagicParams.magicBaseView = magicBaseView; 
     return new MagicEngine(this); 
    } 

    public Builder setVideoPath(String path){ 
     MagicParams.videoPath = path; 
     return this; 
    } 

    public Builder setVideoName(String name){ 
     MagicParams.videoName = name; 
     return this; 
    } 

} 

}

另見.XML

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/camera_surface" 
    android:layout_below="@+id/downloadToolbal" 
    android:layout_weight="1"> 


    <ImageView 
     android:id="@+id/cameraImg" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <com.seu.magicfilter.widget.MagicCameraView 
     android:id="@+id/gl_surface_camera" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</RelativeLayout> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/filter_listView" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:background="@android:color/white" 
    android:padding="@dimen/margin_small" 
    android:scrollbars="none" /> 
+0

查看我的答案。如果您有任何疑問,請告訴我。 –

+0

你好@Andy開發者感謝您的回覆...以及這是有幫助的,但這不是根據我的代碼..你能否請你解釋一下我的代碼真的很感謝你 –

+0

歡迎:)你試過我的建議嗎? –

回答

0

試試這個。這是我的谷歌播放應用程序的工作代碼。

Camera.PictureCallback mPicture = new Camera.PictureCallback() { 
    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 
     File pictureFile = new File(photoPath.toString()); 
     byte[] pictureBytes; 
     Bitmap thePicture; 
     Matrix m; 
     ByteArrayOutputStream bos; 
     BitmapFactory.Options opt; 

     if (pictureFile == null) { 
      return; 
     } 
     try { 
      opt = new BitmapFactory.Options(); 
      opt.inTempStorage = new byte[16 * 1024]; 
      Camera.Parameters parameters = camera.getParameters(); 
      Camera.Size size = parameters.getPictureSize(); 

      int height11 = size.height; 
      int width11 = size.width; 
      float mb = (width11 * height11)/1024000; 

      if (mb > 4f) 
       opt.inSampleSize = 2; 

      thePicture = BitmapFactory.decodeByteArray(data, 0, data.length, opt); 

      if (cameraFront) { 
       m = new Matrix(); 
       m.postRotate(270); 
       Utils.freeMemory(); 
       thePicture = Bitmap.createBitmap(thePicture, 0, 0, thePicture.getWidth(), thePicture.getHeight(), m, true); 

       bos = new ByteArrayOutputStream(); 
       thePicture.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
       pictureBytes = bos.toByteArray(); 
      } else { 
       m = new Matrix(); 
       m.postRotate(90); 
       Utils.freeMemory(); 
       thePicture = Bitmap.createBitmap(thePicture, 0, 0, thePicture.getWidth(), thePicture.getHeight(), m, true); 

       bos = new ByteArrayOutputStream(); 
       thePicture.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
       pictureBytes = bos.toByteArray(); 
      } 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      fos.write(pictureBytes); 
      fos.close(); 
      if (thePicture != null) { 
       if (!thePicture.isRecycled()) { 
        thePicture.recycle(); 
       } 
       thePicture = null; 
      } 
      bos.close(); 
      m = null;  
     } catch (FileNotFoundException e) { 
      Log.error(getClass().getName(), e.toString()); 
     } catch (IOException e) { 
      Log.error(getClass().getName(), e.toString()); 
     } catch (OutOfMemoryError e) { 
      Log.error(getClass().getName(), e.toString()); 
     } 
    } 
}; 
相關問題