[英]Android Custom Camera Pictures when Captured and saved Picture is Totally Black
您好所有Android開發人員。
我正在使用自定義相機(Android NDK),將在實時攝像頭上應用一些濾鏡。 好的,但是當我捕獲圖片並將其保存時,圖片變為全黑圖像。 首先,我將捕獲的圖像轉換為Bitmap並對其進行解碼,為進一步了解,我在這里向您發送我的完整代碼。...
@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" />
試試這個。 這是我的Google Play應用程序的工作代碼。
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());
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.