I am capture image using Camera2 Android. When I capture image from rear camera and apply text over image view. it will show right result. but when I capture image from front camera it is showing text over image in reverse order. i am posting front camera image and rear camera image with code. please help me out solve this problem.

// image save code 

  private class ImageSaver implements Runnable {
        private final File mFile;
        private Image mImage;
        private ICallback mCallback;
        private Bitmap mBitmap;

        ImageSaver(Bitmap bitmap, File file, ICallback callback) {
            mBitmap = bitmap;
            mFile = file;
            mCallback = callback;
        }

        ImageSaver(Image image, File file, ICallback callback) {
            mImage = image;
            mFile = file;
            mCallback = callback;
        }

        @Override
        public void run() {


            if (mImage != null) {
                try {


                    ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
                    byte[] bytes = new byte[buffer.remaining()];
                    buffer.get(bytes);
                    FileOutputStream output = null;
                    try {
                        File file = new File(mFile, "temp_image.jpeg");
                        output = new FileOutputStream(file);
                        output.write(bytes);
                        if (session.isLivePreview()) {
                            DebugLog.e("Live Preview");
                            saveImage(processingBitmap(file.getAbsolutePath()), file.getAbsolutePath());
                        }
                    } catch (IOException iex) {
                        iex.printStackTrace();
                        DebugLog.e("Crash Catch");
                        mCallback.done(iex);
                    } finally {
                        mImage.close();
                        if (null != output) {
                            try {
                                output.close();
                            } catch (IOException ex) {
                                ex.printStackTrace();
                                DebugLog.e("Crash Catch Finally");
                                mCallback.done(ex);
                            }
                        }
                        mCallback.done(null);
                    }


                } catch (Exception ex) {
                    ex.printStackTrace();
                    DebugLog.e("Crash Catch Parent");
                    mCallback.done(ex);

                }

            } else if (mBitmap != null) {

                File file = null;

                try {

                    ByteArrayOutputStream stream = null;
                    byte[] imageByteArray = null;
                    stream = new ByteArrayOutputStream();
                    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    imageByteArray = stream.toByteArray();

                    SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
                    String format = s.format(new Date());

                    file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/myimages/");

                    if (!file.exists()) {
                        try {
                            file.mkdir();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            DebugLog.e("MSG:  OOPS! SOMETHING WENT WRONG");
                        }
                    }

                    file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/myimages/", "stamper_" + format + ".jpg");

                    // save the mirrored byte array
                    FileOutputStream output = null;
                    try {
                        output = new FileOutputStream(file);
                        output.write(imageByteArray);
                    } catch (IOException e) {
                        mCallback.done(e);
                        e.printStackTrace();
                    } finally {
                        if (null != output) {
                            try {
                                output.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            mCallback.done(null);
                            DebugLog.e("Done Bitmap");
                        }
                    }


                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }


        }
    }



//write text over image function 

 private Bitmap ProcessingBitmap(String imagePath) {
        Bitmap bm1 = null;
        Bitmap newBitmap = null;
        final MySessionManager session = new MySessionManager(this);

        // bm1 = BitmapFactory.decodeStream(
        //       getContentResolver().openInputStream(Uri.parse(imagePath)));
        // imagePath = Uri.fromFile(new File(session.getLogo())).toString();
        bm1 = BitmapFactory.decodeFile(imagePath);
        Bitmap.Config config = bm1.getConfig();
        if (config == null) {
            config = Bitmap.Config.ARGB_8888;
        }

        newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), config);
        Canvas newCanvas = new Canvas(newBitmap);
        newCanvas.drawBitmap(bm1, 0, 0, null);

        if (session.isDATETIME()) {
            // DateFormat dateFormat = android.text.format.DateFormat.format(session.getDATEFORMAT(),new Date());
            SimpleDateFormat dateFormat = new SimpleDateFormat(session.getDATEFORMAT(), Locale.ENGLISH);
            String captionString = dateFormat.format(new Date());
            // CharSequence captionString = android.text.format.DateFormat.format(session.getDATEFORMAT(),new Date());

            //String captionString = "Setblue.com";
            if (captionString != null) {
                int x = 0, y = 0;


                TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG);
                paintText.setColor(session.getFontColor());
                paintText.setTextSize(session.getFONTSIZE() * 4);
                paintText.setStyle(Paint.Style.FILL);
                paintText.setTypeface(Typeface.createFromAsset(getAssets(), session.getFontStyle() + ".ttf"));


                Rect rectText = new Rect();
                paintText.getTextBounds(captionString, 0, captionString.length(), rectText);


                if (session.getFontPostion().equalsIgnoreCase(getString(R.string.top_left_horizontal))) {
                    x = 0;
                    y = rectText.height();

                } else if (session.getFontPostion().equalsIgnoreCase(getString(R.string.top_right_horizontal))) {
                    x = newCanvas.getWidth() - rectText.width();
                    y = rectText.height();

                } else if (session.getFontPostion().equalsIgnoreCase(getString(R.string.top_center))) {
                    x = (newCanvas.getWidth() / 2) - (rectText.width() / 2);
                    y = rectText.height();

                } else if (session.getFontPostion().equalsIgnoreCase(getString(R.string.bottom_left_horizontal))) {
                    x = 0;
                    y = newCanvas.getHeight() - 10;

                } else if (session.getFontPostion().equalsIgnoreCase(getString(R.string.bottom_right_horizontal))) {
                    x = newCanvas.getWidth() - rectText.width();
                    y = newCanvas.getHeight() - 10;

                } else if (session.getFontPostion().equalsIgnoreCase(getString(R.string.bottom_center))) {
                    x = (newCanvas.getWidth() / 2) - (rectText.width() / 2);
                    y = newCanvas.getHeight() - 10;

                }
                newCanvas.drawText(captionString,
                        x, y, paintText);
                // newCanvas.translate(0, newCanvas.getWidth()/2);

            } else {

            }
        }


        //location
        if (session.isLOCATION()) {
            // geocoder = new Geocoder(this, Locale.getDefault());
            //locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            // locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);


            //String address = address;
            if (address != null) {
                int x = 0, y = 0;
                TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG);
                paintText.setColor(session.getFontColorLocation());
                paintText.setTextSize(session.getFONTSIZELocation() * 4);
                paintText.setStyle(Paint.Style.FILL);
                paintText.setTypeface(Typeface.createFromAsset(getAssets(), session.getFontStyleLocation() + ".ttf"));


                Rect rectText = new Rect();
                paintText.getTextBounds(address, 0, address.length(), rectText);

                if (session.getFontPostionLocation().equalsIgnoreCase(getString(R.string.top_left_horizontal))) {
                    x = 0;
                    y = rectText.height();

                } else if (session.getFontPostionLocation().equalsIgnoreCase(getString(R.string.top_right_horizontal))) {
                    x = newCanvas.getWidth() - rectText.width();
                    y = rectText.height();

                } else if (session.getFontPostionLocation().equalsIgnoreCase(getString(R.string.top_center))) {
                    x = (newCanvas.getWidth() / 2) - (rectText.width() / 2);
                    y = rectText.height();

                } else if (session.getFontPostionLocation().equalsIgnoreCase(getString(R.string.bottom_left_horizontal))) {
                    x = 0;
                    y = newCanvas.getHeight() - 10;

                } else if (session.getFontPostionLocation().equalsIgnoreCase(getString(R.string.bottom_right_horizontal))) {
                    x = newCanvas.getWidth() - rectText.width();
                    y = newCanvas.getHeight() - 10;

                } else if (session.getFontPostionLocation().equalsIgnoreCase(getString(R.string.bottom_center))) {
                    x = (newCanvas.getWidth() / 2) - (rectText.width() / 2);
                    y = newCanvas.getHeight() - 10;

                }
                newCanvas.drawText(address,
                        x, y, paintText);
                // newCanvas.translate(0, newCanvas.getWidth()/2);

            } else {

            }
        }


        return newBitmap;
    }

    public void saveImage(Bitmap bitmap, String path) {
        //name = name + "." + extension;
        try {
            File file = new File(path);
            String iPath = file.getAbsolutePath();
            FileOutputStream fileOutputStream = new FileOutputStream(iPath);

            //fileOutputStream = openFileOutput(iPath, Context.MODE_PRIVATE);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude", "disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude", "enable");
    }

    @Override
    public void onLocationChanged(Location location) {
        MySessionManager session = new MySessionManager(this);
        Log.d("address", "Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        try {
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            String area = addresses.get(0).getSubLocality();
            String city = addresses.get(0).getLocality();
            String state = addresses.get(0).getAdminArea();
            String country = addresses.get(0).getCountryName();
            Log.d("Area", area);
            Log.d("City", city);
            Log.d("State", state);
            Log.d("Country", country);

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

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude", "status");
    }


}

Front Camera capture image

Front Camera capture image

Rear Camera Capture Image

Rear Camera capture image

0 Answers