捕获并保存的图片完全是黑色的Android自定义相机图片

时间:2017-06-20 11:37:11

标签: android camera android-ndk

欢迎所有Android Dev。

我正在使用自定义相机(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 Class,那么这里是Bellow

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" />

1 个答案:

答案 0 :(得分:0)

试试这个。这是我的谷歌播放应用程序的工作代码。

    private Object responseSignal = new object();
    private string portResponse;
   _session.DataReceived += new SerialDataReceivedEventHandler(PortDataReceived);

static string GetData(int memoryAddress)
{
   if (!_session.IsOpen)
     return "No Connection";

   string toSend = "*A_r_" + memoryAddress.ToString() + "_0" + (char)21;

   foreach (char character in toSend)
     _session.Write(character.ToString());

  if (System.Threading.Monitor.Wait(responseSignal, 10000)) // max time we want to wait to receive the response
   {
       // successful get
   }   
   else
   {
       //  failed to receive the response 
    }
    return portResponse;
  }

  public void PortDataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            lock (responseSignal)
            {
                string tmpPortResponse = _session.ReadExisting();
                portResponse += tmpPortResponse;


                //Complete the response only when you get the end character
                if (tmpPortResponse.Contains('.'))
                {
                    // signal allows a waiting SendMessage method to proceed
                    System.Threading.Monitor.Pulse(responseSignal);
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
    }